Page 1 of 1

Database connection closed in unload(), still closed after the next load()

Posted: Wed Nov 25, 2015 3:24 pm
by Mahi
I've been working on a new Hero-Wars based Warcraft mod for CS:GO with Predz. I took an approach where I create a Database class which connects to the SQL database in its __init__. The database object is initialized in the plugin's load() function. In the unload() function, I call the database's close() method which commits changes, closes the connection, and sets the connection to None.

It all works fine, until I reload the plugin, which is when I get an error that the database object's connection attribute is NoneType object (or if I don't set it to None in Database.close(), I get a "cannot operate on closed database" error):

Code: Select all

sp reload wcgo
[SP] Unloading plugin 'wcgo'...
[SP] Successfully unloaded plugin 'wcgo'.
[SP] Loading plugin 'wcgo'...
[SP] Successfully loaded plugin 'wcgo'.

[SP] Caught an Exception:
Traceback (most recent call last):
  File '..\addons\source-python\packages\source-python\events\listener.py', line
 92, in fire_game_event
    callback(game_event)
  File '..\addons\source-python\plugins\wcgo\wcgo.py', line 103, in _on_player_s
pawn
    database.save_player(player)
  File '..\addons\source-python\plugins\wcgo\database.py', line 50, in save_play
er
    self.connection.execute(

AttributeError: 'NoneType' object has no attribute 'execute'

Here's the GitHub repository: https://github.com/Warcraft-GO-Team/Warcraft-GO
Here's the Database class: https://github.com/Warcraft-GO-Team/Warcraft-GO/blob/master/addons/source-python/plugins/wcgo/database.py
Here are the load() and unload() functions: https://github.com/Warcraft-GO-Team/Warcraft-GO/blob/master/addons/source-python/plugins/wcgo/wcgo.py#L38-L66

I've been trying to figure this out for quite a while. It's as if the plugin's load() is called before the previous unload() is finished, but I tried adding prints and that was not true at all. I also tried "sp unload wcgo" and then wait a few seconds before "sp load wcgo" without any success, so it's not that. Somehow the database object is not reset when I reload the plugin. Any ideas?

Posted: Wed Nov 25, 2015 10:41 pm
by Mahi
Apparently every time I reload the plugin, I get one more error (although the exact same error) from player_spawn, and always one player_spawn succeeds. So first time I load the plugin -> player_spawn succeeds. Second load -> one player_spawn succeeds and one error is raised due to connection being None. On third load -> one success and two failures.

Could this be a bug about the old player_spawn callback not unregistering for one reason or another?

Posted: Wed Nov 25, 2015 10:50 pm
by Mahi
Solved! The issue was caused because I had two functions with the same name, and apparently AutoUnload unloads things by looping over the names in modules. I wasn't aware of this and thought it registers every callback into a list and then loops through that list to unload them (why doesn't it do it that way?).