fix(queue): refuse a concurrency an exclusive consumer cannot survive - #230
Merged
levivannoort merged 3 commits intoSep 9, 2026
Merged
Conversation
A NATS connection is one socket with one shared read pump. Above one coroutine
the receive loop parks inside a read on that socket while the coroutines
processing earlier messages commit, reject or extend on the same socket, and
Swoole ends the worker on the first overlap:
Swoole\Error: Socket#5 has already been bound to another coroutine#2,
reading of the same socket in coroutine#3 at the same time is not allowed
Reproduced against nats:2.11 on a Swoole build: one coroutine acknowledges all
15 publishes, two coroutines die on the first message. The existing guard in
start() only covers multi-queue workers sharing a consumer, so a single-queue
job at maxCoroutines above one reached the loop and crashed under traffic.
Consumer\Exclusive marks a consumer that owns its transport, Broker\Nats
carries it, and start() refuses the configuration before any loop runs, naming
the queue and the cap it was given.
The marker is what gates this, not the concurrency. Broker\Redis does not carry
it and keeps its caps: Connection\Locking already serialises the coroutines
sharing one connection.
levivannoort
requested review from
ChiragAgg5k,
abnegate and
lohanidamodar
as code owners
September 7, 2026 21:54
…d from main Two violations, both in tests. Mine: Rector's AssertEmptyNullableObjectToAssertInstanceofRector rejects assertNotNull() on a nullable object. assertInstanceOf() is also the stronger assertion here -- the refusal has to be an Exception, not merely not-null. Inherited: NatsBrokerTest asserts on Subscription::$subject, which is nullable, so StringCastAssertStringContainsStringRector wants the cast. That violation is already on main (it arrived in 68bc230, after this branch was cut) and the merge brings it in, so this branch cannot go green without it. Verified: pint, phpstan and rector clean on the package, ServerJobsTest 11/11, NatsBrokerTest 31 with only the pre-existing Swoole\Coroutine\run() error from the extension missing on the host.
loks0n
approved these changes
Sep 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The break
A NATS connection is one socket with one shared read pump.
Broker\Nats's docblock has always said so, andtests/Queue/servers/Nats/worker.phphardcodesjob('nats', 1)because of it. Nothing enforced it.Above one coroutine,
consumeBound()parks the receive loop inside a read on that socket while the coroutines processing earlier messagescommit(),reject()or extend on the same socket. Swoole does not tolerate the overlap:Reproduced against
nats:2.11on a Swoole build, N coroutines publishing over one shared connection:expected=15 acked=15 errors=0It is not a slow degradation or a silent miscorrelation. The worker dies immediately, and it dies under traffic rather than at boot.
The existing guard in
Server::start()only covers multi-queue workers sharing a consumer (count($this->jobs) > 1). A single-queue job atmaxCoroutinesabove one walked straight past it.The fix
Consumer\Exclusivemarks a consumer that owns its transport.Broker\Natscarries it.Server::start()refuses before any loop runs:The marker gates this, not the concurrency.
Broker\Redisdoes not carry it and keeps its caps unchanged —Connection\Lockingalready serialises the coroutines sharing one connection. A Redis worker atjob('v1-functions', 8)behaves exactly as before, and there is a test pinning that.Tests
Three added to
ServerJobsTest:testStartRefusesConcurrencyOnAnExclusiveConsumer— asserts the refusal and that$adapter->consumed === [], so the refusal is proven to land before any loop is handed a cap it cannot survive, rather than only pinning the message wording.testStartAcceptsOneCoroutineOnAnExclusiveConsumer— NATS still runs at 1.testStartKeepsConcurrencyOnAConsumerWithoutTheMarker— the Redis shape stays at 8.Seen red: replacing the guard condition with
if (false)fails onlytestStartRefusesConcurrencyOnAnExclusiveConsumer(the concurrency must be refused / Failed asserting that null is not null) and leaves the other ten green, which is what shows the guard is capability-gated rather than blanket.ServerJobsTest11/11. Unit suite 49 tests, 4 errors, allSwoole\Coroutine\run()missing on the host inSwooleContextTestand present before this change. PHPStan and Pint clean,bin/monorepo validatepasses.Note for callers
Anyone who sets a coroutine cap from configuration and can point a worker at NATS should cap it at 1 for that transport, or they trade a crash loop for this refusal. Appwrite Cloud already forces
maxCoroutinesto 1 on anats://DSN and warns; this makes the library safe for callers that do not.