Error with the Neo4j Python driver : ImportError: sys.meta_path is None, Python is likely shutting down

Hello,

I'm using the Neo4j Python Driver version 5.9 with a Neo4j DBMS version 5.9 and I got the following error at the end of my script. The error happens once my script is finished so everything is fine but I would prefer not to have an error :slight_smile:

Exception ignored in: <function Driver.__del__ at 0x000001B9284BAA60>
Traceback (most recent call last):
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\driver.py", line 496, in __del__
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\driver.py", line 557, in close
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_pool.py", line 483, in close
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_pool.py", line 415, in _close_connections
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 906, in close
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 758, in _send_all
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_common.py", line 145, in flush
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_async_compat\util.py", line 111, in callback
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 837, in _set_defunct_write
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 840, in _set_defunct
ImportError: sys.meta_path is None, Python is likely shutting down
Exception ignored in: <function Bolt.__del__ at 0x000001B928498310>
Traceback (most recent call last):
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 169, in __del__
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 906, in close
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 758, in _send_all
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_common.py", line 145, in flush
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_async_compat\util.py", line 111, in callback
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 837, in _set_defunct_write
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 840, in _set_defunct
ImportError: sys.meta_path is None, Python is likely shutting down

Best regards,
Cobra

Interesting :eyes:

The easiest thing would be to make sure the driver gets closed properly. Something like

import neo4j


with neo4j.GraphDatabase.driver(...) as driver:
    ...  # do the work here

or manually:

import neo4j


driver = neo4j.GraphDatabase.driver(...)
try:
    ...  # do the work here
finally:
    driver.close()

I'm investigating if there's a way to avoid those unclean shut-down errors. As indicated by "Python is likely shutting down", this only happens when the driver is not closed and then garbage collected on interpreter shutdown. This should also emit a deprecation warning (these are supresed by default by Python) because in the future (version 6.0, probably) deleting a driver without closing it will just cause a resource warning without attempting to close the driver like it does right now. So it would then much more behave like Python sockets do. In that version, the reported error would just not occur. So I won't put all too much investigation work into it as 1) there's a workaround: close the driver and 2) in the future it will not happen anymore. But maybe there's a long hanging fruit (i.e., improvement) here.

1 Like

If I interpret that snipped correctly, you only use session as a context manager (with statement). But I don't see what you do with the driver.

Yeah you right, I saw them instantaneously while re-reading it :slight_smile:

Thank you for your help :slight_smile:

I just need to close the driver in the right places in my code.

Could you kindly try if this PR gets rid of the error (before the change of closing the driver properly)?

You can install that PR branch with pip install -U git+https://github.com/robsdedude/neo4j-python-driver.git@fix-unclean-interpreter-shutdown (maybe use a virtualenv for that test to not destroy your dependencies :D). It would be greatly appreciated!

1 Like

When I tried, I got multiple errors:

  • ssl.SSLError: Underlying socket connection gone (_ssl.c:2409)
  • NameError: name 'open' is not defined
  • Message: "Failed to write data to connection IPv4Address(('xxxxxxxxxxxxxxxxxxxx', 7687)) (ResolvedIPv4Address(('xxxxxxxxxxxxxxx', 7687)))"
1 Like

Thanks a bunch for trying it out! Do you get those errors on shutdown? Or does the driver die right away? Either way, could you provide me with the stack traces? Meanwhile I'll try to reproduce the errors locally myself.

Hello @rouven_bauer :slight_smile:

I guess I got the error on shutdown since I can see the result of the query. Here is the stack trace (I have hidden only the IP and the domain name):

--- Logging error ---
Traceback (most recent call last):
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_common.py", line 141, in flush
    self.socket.sendall(data)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_async_compat\network\_bolt_socket.py", line 491, in sendall
    return self._wait_for_io(self._socket.sendall, data)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_async_compat\network\_bolt_socket.py", line 463, in _wait_for_io
    return func(*args, **kwargs)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\ssl.py", line 1204, in sendall
    v = self.send(byte_view[count:])
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\ssl.py", line 1173, in send
    return self._sslobj.write(data)
ssl.SSLError: Underlying socket connection gone (_ssl.c:2409)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\logging\handlers.py", line 69, in emit
    if self.shouldRollover(record):
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\logging\handlers.py", line 183, in shouldRollover
    self.stream = self._open()
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\logging\__init__.py", line 1176, in _open
    return open(self.baseFilename, self.mode, encoding=self.encoding)
NameError: name 'open' is not defined
Call stack:
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\driver.py", line 496, in __del__
    self.close()
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\driver.py", line 557, in close
    self._pool.close()
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_pool.py", line 488, in close
    self._close_connections(connections)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_pool.py", line 420, in _close_connections
    connection.close()
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 905, in close
    self._send_all()
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 758, in _send_all
    if self.outbox.flush():
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_common.py", line 145, in flush
    Util.callback(self.on_error, error)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_async_compat\util.py", line 111, in callback
    return cb(*args, **kwargs)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 837, in _set_defunct_write
    self._set_defunct(message, error=error, silent=silent)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 847, in _set_defunct
    log.error(message)
Message: "Failed to write data to connection ResolvedIPv4Address(('***.***.**.***', 7687)) (ResolvedIPv4Address(('***.***.**.***', 7687)))"
Arguments: ()
Failed to write data to connection ResolvedIPv4Address(('***.***.**.***', 7687)) (ResolvedIPv4Address(('***.***.**.***', 7687)))
--- Logging error ---
Traceback (most recent call last):
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_common.py", line 141, in flush
    self.socket.sendall(data)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_async_compat\network\_bolt_socket.py", line 491, in sendall
    return self._wait_for_io(self._socket.sendall, data)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_async_compat\network\_bolt_socket.py", line 463, in _wait_for_io
    return func(*args, **kwargs)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\ssl.py", line 1204, in sendall
    v = self.send(byte_view[count:])
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\ssl.py", line 1173, in send
    return self._sslobj.write(data)
ssl.SSLError: Underlying socket connection gone (_ssl.c:2409)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\logging\handlers.py", line 69, in emit
    if self.shouldRollover(record):
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\logging\handlers.py", line 183, in shouldRollover
    self.stream = self._open()
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\logging\__init__.py", line 1176, in _open
    return open(self.baseFilename, self.mode, encoding=self.encoding)
NameError: name 'open' is not defined
Call stack:
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\driver.py", line 496, in __del__
    self.close()
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\driver.py", line 557, in close
    self._pool.close()
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_pool.py", line 488, in close
    self._close_connections(connections)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_pool.py", line 420, in _close_connections
    connection.close()
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 905, in close
    self._send_all()
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 758, in _send_all
    if self.outbox.flush():
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_common.py", line 145, in flush
    Util.callback(self.on_error, error)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_async_compat\util.py", line 111, in callback
    return cb(*args, **kwargs)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 837, in _set_defunct_write
    self._set_defunct(message, error=error, silent=silent)
  File "C:\Users\Cobra\anaconda3\envs\viziball\lib\site-packages\neo4j\_sync\io\_bolt.py", line 847, in _set_defunct
    log.error(message)
Message: "Failed to write data to connection IPv4Address(('**********', 7687)) (ResolvedIPv4Address(('***.***.**.***', 7687)))"
Arguments: ()
Failed to write data to connection IPv4Address(('**********', 7687)) (ResolvedIPv4Address(('***.***.**.***', 7687)))

Best regards,
Cobra

1 Like

Thanks once more. So now the error moved further down the call stack as there's some logging involved which now fails as the interpreter shuts down. I don't think I can fix that any other way but by stopping to properly shut down the driver in __del__ in 6.0.

I assume you did not run your python script with -X dev (see Python Development Mode — Python 3.11.4 documentation) which among other things enables DeprecationWarnings and ResourceWarnings. Generally that's a good thing to do while developing with Python. In this case it should've given you a clue about the underlying issue (unclosed driver).

Thank you to you too @rouven_bauer :slight_smile:

I did not know the -X dev option, I learned something new :slight_smile:

Best regards,
Cobra