Avoid GC-induced reentrant calls to add_callback()#1875
Avoid GC-induced reentrant calls to add_callback()#1875pitrou wants to merge 1 commit intotornadoweb:masterfrom
Conversation
|
My initial reaction is the same as @ajdavis: it doesn't seem sustainable to disable GC around all uses of a lock that might be acquired in a destructor. There's a second use of The How exactly is the destructor getting run during This also looks like a substantial performance penalty on the critical path. The background thread proposed in the other thread may end up needing the same GC-disabling dance, but at least it localizes the impact to applications needing complex destructors instead of affecting everything that uses tornado. |
|
Agreed. And the background-thread technique, if you're very very careful, is safe without disabling the GC. (We have |
|
Hi Ben,
I agree on the principle. But
Unfortunately not. The cycle GC can run basically whenever it wants. More exactly, on CPython, it is whenever the number of the GC allocations goes above a certain threshold (the exact computation is in gcmodule.c). So allocating a simple tuple can trigger a GC run. Needless to say, this can happen in any non trivial piece of code.
You're right, that's a real bug here :-/ |
How do you ensure the |
|
@bdarnell, I have a question: since |
|
By the way, I agree that the fact this PR introduces a race condition in |
|
Ok, I've submitted a lockless implementation in #1876. |
|
@pitrou you asked me,
My technique is a lot like the one you showed in your PR. Mine's a little less sophisticated, it's just: This works for small numbers of tasks. If the cyclic GC is executed on this thread that's ok, more tasks will appear in the list and the "while" loop will run a little longer. If this happens rarely, then the "while" loop will always finish eventually. Your deque implementation is much more appropriate for Tornado, since it only pops the callbacks that were present in the queue at the beginning of the consumer loop, and defers new callbacks for later. |
|
Thanks. I'm closing this PR now, as disabling the GC is clearly not a good solution. |
Fixes #1874.