Skip to content

Terminate netbox client threadpool on exit#67

Merged
azryve merged 1 commit intomainfrom
fix/terminate-threadpool-atexit
Jan 27, 2026
Merged

Terminate netbox client threadpool on exit#67
azryve merged 1 commit intomainfrom
fix/terminate-threadpool-atexit

Conversation

@azryve
Copy link
Contributor

@azryve azryve commented Jan 23, 2026

From the mp.ThreadPool documentation.

resources must also be properly managed, either by using the pool as a context manager or by calling close() and terminate() manually.

Without explicit close() or terminate() it can produce exceptions on exit when __del__ is called, before the fds were opened:

Exception ignored while calling deallocator <function Pool.__del__ at 0x10a340bf0>: Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/multiprocessing/pool.py", line 271, in __del__
    self._change_notifier.put(None)
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/multiprocessing/queues.py", line 397, in put
    self._writer.send_bytes(obj)
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/multiprocessing/connection.py", line 206, in send_bytes
    self._send_bytes(m[offset:offset + size])
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/multiprocessing/connection.py", line 444, in _send_bytes
    self._send(header + buf)
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/multiprocessing/connection.py", line 400, in _send
    n = write(self._handle, buf)
OSError: [Errno 9] Bad file descriptor

Much cleaner would be to a use context manager but BaseNetboxClient does not provide one, instead doing init_pool in the constructor. This leaves atexit as a least involved option to prevent this annoying traceback on the shutdown.

From the mp documentation: https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.ThreadPool
> resources must also be properly managed, either by using the pool as a context manager or by calling close() and terminate() manually.

Without explicit close() or terminate() it can produce exceptions on exit when __del__ is called, before the fds were opened:

Exception ignored while calling deallocator <function Pool.__del__ at 0x10a340bf0>:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/multiprocessing/pool.py", line 271, in __del__
    self._change_notifier.put(None)
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/multiprocessing/queues.py", line 397, in put
    self._writer.send_bytes(obj)
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/multiprocessing/connection.py", line 206, in send_bytes
    self._send_bytes(m[offset:offset + size])
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/multiprocessing/connection.py", line 444, in _send_bytes
    self._send(header + buf)
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/multiprocessing/connection.py", line 400, in _send
    n = write(self._handle, buf)
OSError: [Errno 9] Bad file descriptor

Much cleaner would be to a use context manager but BaseNetboxClient does not provide one, instead doing init_pool in the constructor.
This leaves atexit as a least involved option to prevent this annoying traceback on the shutdown.
@azryve azryve merged commit 5499551 into main Jan 27, 2026
0 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants