Bug report
Bug description:
queue.SimpleQueue.put is implemented in C; it's implemented by _queue_SimpleQueue_put_impl in Modules/_queuemodule.c. Reading the code this morning led me to me suspect it was frequently leaking a reference (!); I finally figured out what it's doing, and no, it only leaks rarely, if memory allocation fails. Still: I think the code could be polished up a little and made easier to follow with no loss of runtime efficiency.
From a high level, _queue_SimpleQueue_put_impl attempts first to give the "item" being put to a waiting thread, if any. If there are no eligible waiting threads, it attempts to put the "item" onto a ring buffer. This may involve allocating memory, which could fail. The function eagerly pre-increfs "item", assuming the reference will be consumed by either the handoff to the other thread or by the queue. If both those operations fail, it leaks this eagerly-created reference.
Admittedly, if this re-allocation fails, the process is likely over anyway, and the leak of this one object is immaterial. Still, I propose a small rewrite both to remove the refleak, and to improve readability of the code.
This code shipped with 3.13, and is in 3.14, 3.15, and trunk. If we accept this PR, I'd propose to also backport to the recently-beta 3.15. But personally I wouldn't bother with a backport to earlier versions.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
Bug report
Bug description:
queue.SimpleQueue.put is implemented in C; it's implemented by _queue_SimpleQueue_put_impl in Modules/_queuemodule.c. Reading the code this morning led me to me suspect it was frequently leaking a reference (!); I finally figured out what it's doing, and no, it only leaks rarely, if memory allocation fails. Still: I think the code could be polished up a little and made easier to follow with no loss of runtime efficiency.
From a high level, _queue_SimpleQueue_put_impl attempts first to give the "item" being put to a waiting thread, if any. If there are no eligible waiting threads, it attempts to put the "item" onto a ring buffer. This may involve allocating memory, which could fail. The function eagerly pre-increfs "item", assuming the reference will be consumed by either the handoff to the other thread or by the queue. If both those operations fail, it leaks this eagerly-created reference.
Admittedly, if this re-allocation fails, the process is likely over anyway, and the leak of this one object is immaterial. Still, I propose a small rewrite both to remove the refleak, and to improve readability of the code.
This code shipped with 3.13, and is in 3.14, 3.15, and trunk. If we accept this PR, I'd propose to also backport to the recently-beta 3.15. But personally I wouldn't bother with a backport to earlier versions.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs