New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emit ResourceWarning in multiprocessing Queue destructor #74357
Comments
A multiprocessing Queue object managers multiple resources:
If a Queue is not cleaned up properly, your application may leak many resources. Try attached queue_leak.py to see an example "leaking a thread". I suggest to emit a ResourceWarning warning in Queue destrutor. I don't know what should be the test to decide if a warning must be emitted?
Other examples of objects emitting ResourceWarning:
|
Example: haypo@selma$ ./python queue_leak.py Note: queue_leak.py resource check is based on test.support.reap_threads. |
Oh, I forgot that I hitted this issue while analyzing issue bpo-30131: test_logging leaks a "dangling" thread. It took me a while to find multiprocessing queues in the big test_logging file! |
See also issue bpo-30244: Emit a ResourceWarning in concurrent.futures executor destructors. |
The thread seems to be stopped when the Queue object is finalized: # Send sentinel to the thread queue object when garbage collected
self._close = Finalize(
self, Queue._finalize_close,
[self._buffer, self._notempty],
exitpriority=10
) I don't think the other resources (pipe, lock, semaphore) need explicit cleaning. |
Another example: "The problem is that multiprocessing.Queue.join_thread() does nothing since the thread wasn't started by a subprocess." |
I don't understand what this means. Can you clarify a bit? |
Specifically "the thread wasn't started by a subprocess"... |
I'm talking about this check in Queue._start_thread() of multiprocessing.queues: created_by_this_process = (self._opid == os.getpid())
if not self._joincancelled and not created_by_this_process:
self._jointhread = Finalize(
self._thread, Queue._finalize_join,
[weakref.ref(self._thread)],
exitpriority=-5
) |
Let's discuss created_by_this_process in bpo-30886. This issue is more about adding or not a ResourceWarning. |
I don't think a ResourceWarning should be emitted. There is no risk of data loss or resource leak if you don't close a multiprocessing Queue explicitly. Actually, in the real world, I don't think I've ever seen code that closes queues explicitly. |
I'm willing to close this issue, as I don't think a ResourceWarning is appropriate here. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: