Repository navigation
Conversation
Automation signalsActivity patterns show signs of automation. Evidence
Last 5 PRs:
This is an automated analysis by AgentScan |
d941f85 to
1239a33
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files |
1239a33 to
1d4d6d8
Compare
d6cc705 to
c1a87ea
Compare
rm-you
left a comment
There was a problem hiding this comment.
Updated in 5e85181 and c1a87ea, with the follow-up fixes kept as separate commits. Stop now handles late shrink requests and full queues; growth cannot escape shutdown, interrupted preparation can be retried, and restart is rejected until the previous stop finishes cleanup. #859 remains independent.
The three new regressions failed before the fixes and passed 50 repetitions each afterward. The complete local suite passes (237 passed, 3 existing expected failures), as do the pre-commit checks. I ran a local HTTP benchmark and a comparison with the main branch, and it completed almost 700k requests without errors or a material throughput regression.
c1a87ea to
634e7b7
Compare
6d44fe5 to
7ce9e80
Compare
shrink() subtracted the shutdowns that earlier calls had queued from the amount it was asked for, before it removed the threads that had already exited. So a call made before an earlier call's threads were reaped was mostly ignored: four shrink(20) calls in a row retired 20 of 90 threads, and 40 when made a second apart. The idle property already leaves those threads out, so a caller that sizes the amount from it had them subtracted twice. The threads above the minimum were also counted with those already shutting down: shrink(40), then shrink(80), on 50 threads with a minimum of 10 retired all 50, and left 30 requests queued. The pending shutdowns also had to match the requests in the queue exactly, and could not: a put() interrupted while it waited for room in the queue left one with no request, reaping dropped one for each thread that had exited, whatever the reason, and two calls at once could ask the same threads. The pool now keeps the retirement requests that are due, under a lock. shrink() reserves up to the amount asked, among the threads above the minimum that earlier calls have not asked to retire. It does so inside the try block that takes back the requests it has not queued, so an interrupt anywhere leaves nothing due without a request, and never touches another call's. A request only wakes a worker: a worker that takes one retires if it is still due and the pool keeps its minimum without it, and otherwise goes on serving. So a request that an interrupted call took back, or one that outlived a thread that exited for another reason, changes nothing. idle reads the threads and the retirements due under the same lock. A worker counts the threads that still serve by their ready flag, since before Python 3.13 is_alive() can fail when two threads call it at once for a thread that is exiting. stop() is unchanged. Assisted-By: Anthropic Claude Opus 5.5 Assisted-By: OpenAI Codex Signed-off-by: Adam Harwell <flux.adam@gmail.com>
7ce9e80 to
1fdc3db
Compare
This takes the reviewer's wording for the docstrings and comments of the retirement checks and the idle test, and the test's new name. The _get_request() docstring keeps "shutdown request", since that is what the method returns, for a retirement too. Assisted-By: Anthropic Claude Opus 5.5 Signed-off-by: Adam Harwell <flux.adam@gmail.com>
stop() queued a shutdown request for every thread in its list, and each request ended whichever thread took it. A thread already shutting down, such as one that shrink() had retired, takes none, and neither does a worker that stops its own pool on an interrupt, such as a KeyboardInterrupt from the application. Their requests stayed in the queue, as did those that shrink() queued while the pool stopped. The next start() lost its new threads to them and waited for them forever. A dead thread still in the list also got a request, which could wait for room in a bounded queue forever, and an interrupted stop() could not be repeated, as it had emptied the list. stop() now ends the pool's run. The retirements that shrink() asked for are no longer due, and their requests do nothing in a later run. A thread shuts down on a stop request only if its run has ended, so a request that a stop() left in the queue does nothing to the threads of a later run. stop() queues one request for each live thread that has not taken one yet, even when the queue is full of connections, so it always reaches the timeout that closes stuck connections. The threads that still run when it returns stay listed, so that a later stop() ends them, and start() refuses to start the pool again until they have exited. grow() and start() take the pool's lock, so stop() also ends the threads that a grow() call was starting. grow() refuses while a thread of an ended run has not taken its stop request, which a new thread could otherwise take and drop. Assisted-By: Anthropic Claude Opus 5.5 Assisted-By: OpenAI Codex Signed-off-by: Adam Harwell <flux.adam@gmail.com>
A worker that called stop() on its own pool joined the other threads, so two workers that stopped the pool at once each waited for the other. stop() also dropped the worker that called it from the pool's threads while that worker still handled its connection, so start() could start new threads, one of which could take the stop request that the old worker still needed. And grow() could start a thread while stop() still waited for the old ones, which stop() then left running. A lock now lets one stop() call at a time end the threads. A thread of the pool that finds a call under way returns at once, since that call may be waiting for it to exit. Any other caller waits for that call to finish, then ends the threads still running within what is left of its own timeout, so it still returns only once every thread has exited. grow() and start() refuse while the lock is held. An interrupted call releases the lock and leaves the threads that still run listed, so the next call goes on from there. A worker that ends the threads waits for them no longer than the timeout, closes the connections of those still running, and does not wait for them after that; without a timeout, it waits for none. It stays among the pool's threads until it exits, so the pool starts again only once it has, and grow() refuses meanwhile. Assisted-By: Anthropic Claude Opus 5.5 Signed-off-by: Adam Harwell <flux.adam@gmail.com>
stop() took the pool's lock to tidy up its list of threads, and only then released the lock that marks a stop() under way. An interrupt while it waited for the pool's lock, such as a second Ctrl-C while a worker held it, skipped the release. Every later stop() then waited for that lock forever, and grow() and start() kept refusing, even once every thread had exited. The release now has a finally of its own. The list of threads may then keep some that have exited, which start() and the next stop() forget anyway. Assisted-By: Anthropic Claude Opus 5.5 Signed-off-by: Adam Harwell <flux.adam@gmail.com>
put_shutdown() let a stop request into a full connection queue by using queue.Queue internals: its not_empty condition, _put() and unfinished_tasks. Those are private, and under Eventlet's monkey patching queue.Queue is Eventlet's own class, which lacks them, so every stop() raised AttributeError. stop() now uses put(), as on main. Since only one stop() call at a time queues requests, two calls can no longer fill a bounded queue with requests for each other's threads. What is left is what main does too: with a bounded queue full of connections and every thread stuck on one, stop() waits for room before it can reach the timeout. Assisted-By: Anthropic Claude Opus 5.5 Signed-off-by: Adam Harwell <flux.adam@gmail.com>
6684d7d to
0329e2d
Compare
Note
Second PR in the thread-pool series: #859 → #882. It includes #859 until that merges.
This PR's own changes: https://github.com/cherrypy/cheroot/compare/eb62843a33c9cb4e100ae91f3f94ba56ce22ee77..0329e2df12048eb007e4e7a59e7783d5e3ace92f
What kind of change does this PR introduce?
What do these changes do?
Stopping a pool could leave shutdown requests in its queue.
stop()queued one for every thread in its list, and each ended whichever thread took it. But a thread thatshrink()had already retired takes none, and neither does a worker that stops its own pool on an interrupt, such as aKeyboardInterruptfrom the application. Requests thatshrink()queued while the pool stopped stayed too. When the pool started again, its new threads took those requests and exited, andstart()waited for them forever. A dead thread still in the list also got a request, which could wait forever for room in a bounded queue. And an interruptedstop()could not be repeated, because it had already emptied the list.Now each thread records the run of the pool that it serves, and
stop()ends the run:shrink()asked for are no longer due.stop()left in the queue does nothing to the threads of a later run, and nothing has to be removed from the queue.stop()queues one request for each live thread that has not taken one yet.stop()returns stay in the list, so a laterstop()ends them.grow()andstart()take the pool's lock, sostop()also ends the threads that agrow()call was starting.grow()andstart()refuse untilstop()has returned, sincestop()would not know of a new thread.grow()also refuses while a thread of the ended run has not taken its stop request, since a new thread could otherwise take it.stop()call at a time ends the threads. A worker that callsstop()while another call does returns at once, since that call may be waiting for it to exit. Any other caller waits for that call, then ends what is left, within what remains of its own timeout. So workers that stop the pool together no longer wait for each other until the timeout, and a second call from outside the pool still returns only once every thread has exited.put(), as onmain. Since onestop()call at a time queues them, two calls cannot fill a bounded queue with requests for each other's threads.Are there changes in behavior for the user?
start()raisesRuntimeErrorwhile a thread of the last run still runs, as it did whenever the list was not empty, including a worker that stopped its own pool, and untilstop()has returned.stop()called from one of the pool's workers waits for the other threads no longer than its timeout, closes the connections of those still running, and returns without waiting for them to exit. Without a timeout, it returns at once.stop()call that comes while another runs returns at once if it is from one of the pool's workers. From any other thread, it waits for that call to finish, then ends the threads still running. Its timeout counts from its own start, including that wait. While it waits, it leaves closing connections to the call under way, so behind a call without a timeout it waits as long as that call does.qsizeshows, until it starts again. Its first idle thread then drops it.grow()raisesRuntimeErroruntilstop()has returned. After that, it starts threads in the new run, and a laterstop()ends them.stop()still waits for room before it reaches its timeout, as onmain. That is ThreadPool.stop() waits for room in a full queue before it closes stuck connections #889.Is it a substantial burden for the maintainers to support this?
It adds one run number to the pool and to each worker, a check when a worker takes a stop request, and a lock that the
stop()call under way holds.This PR's own changes are four commits: leftover requests and restarts, one
stop()call at a time, a fix so that an interrupt duringstop()'s final cleanup cannot leave it marked as under way, and stop requests queued with the queue's ownput()again, so thatstop()works under Eventlet's monkey patching. They take in the follow-ups from four independent reviews (stop requests and a full queue,grow()duringstop(), a test, two workers stopping the pool at once, a worker that stops its pool before a restart, a worker'sstop()and stuck connections,grow()orstart()beforestop()returns, and onestop()call at a time).Of the 24 new tests (32 cases), 17 fail or hang on #859's code. The other 7 cover what #859's code already gets right and the new
stop()must keep:stop()ends every thread whatevershrink()counted, a worker that stops its own pool exits, a thread whose join was interrupted exits later, a worker that stops its own pool still closes the connections that threads are stuck on after the timeout, workers that stop the pool together do not wait for each other, a worker'sstop()during another call returns at once, and ashrink()interrupted after astop()leaves the next run's retirements alone. Two defensive checks have no test that fails without them: a run check in the serving count, and marking a thread that takes a stop request so that a repeatedstop()does not ask it again. Each matters only in orderings that a test cannot set up without patching the pool's internals.Stop requests go in with the queue's own
put()because an earlier version'sput_shutdown()relied onqueue.Queueinternals that Eventlet's monkey-patchedqueue.Queuelacks: oslo.messaging's HTTP-RPC driver tests, which run under Eventlet, failed 31 of 248 withAttributeErroron that version, and pass with this one, also with #858 and #881 merged in.Local benchmark of an earlier version of this series (#859 and #882 together; since then, the only change on the per-connection path is one
isinstance()check), the previous version of both, andmain, on Python 3.11 on one machine. The server (10 worker threads) and the client (8 threads) ran in separate processes, for 6 interleaved rounds of 8 seconds each: 997,184 requests, no errors.main: this series +0.3% with keep-alive and +1.5% with a new connection per request; the previous version -0.5% and -0.6%. Rounds varied by about ±3%, so none of these differences is real.stop()plusstart()took about 1.1 ms with all three.grow(20)andshrink(20)cycle took about 2.9 ms with both versions of the fix. Onmainit hangs on the bug fixed here.Overlapping
stop()calls with the default timeout, 5 trials each, real worker threads, againstmainand the previous version of this PR:mainstop()during an outside onestop()during a worker'sStop and start at 1, 16 and 64 threads, grow and shrink cycles, and 50 ms and 250 ms drains match
mainwithin 0.2 ms. The latest change does not touch the per-connection path. An HTTP run after it showed no difference from the previous version, but this machine was too noisy for that run (trials varied by about ±10%) to tell differences of 1 to 2%.Related issue number
Fixes #861, with #859.
Checklist
Tip
New to open source contributions? Making a lovely PR is a great read.
and description in grammatically correct, complete sentences
docs/) and inline docstrings reflect the changes(see good commit messages)
(can be done once the PR number is known; alternatively, you can
use a related issue number if one exists)
code of conduct
Prepared with Anthropic Claude Opus 5.5 on behalf of Adam Harwell (@rm-you). Several regression tests build on ones written with OpenAI Codex for the previous version of this PR.