Skip to content

fix(queue): refuse a concurrency an exclusive consumer cannot survive - #230

Merged
levivannoort merged 3 commits into
mainfrom
fix/queue-refuse-concurrent-exclusive-consumer
Sep 9, 2026
Merged

fix(queue): refuse a concurrency an exclusive consumer cannot survive#230
levivannoort merged 3 commits into
mainfrom
fix/queue-refuse-concurrent-exclusive-consumer

Conversation

@levivannoort

Copy link
Copy Markdown
Contributor

The break

A NATS connection is one socket with one shared read pump. Broker\Nats's docblock has always said so, and tests/Queue/servers/Nats/worker.php hardcodes job('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 messages commit(), reject() or extend on the same socket. Swoole does not tolerate the 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, N coroutines publishing over one shared connection:

coroutines result
1 expected=15 acked=15 errors=0
2 fatal on the first message
4 fatal on the first message
8 fatal on the first message

It 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 at maxCoroutines above one walked straight past it.

The fix

Consumer\Exclusive marks a consumer that owns its transport. Broker\Nats carries it. Server::start() refuses before any loop runs:

Queue 'v1-functions' is registered with job('v1-functions', 8), but its consumer Utopia\Queue\Broker\Nats drives a single socket that only one coroutine may read at a time. Register it as job('v1-functions', 1) and add replicas for throughput.

The marker gates this, not the concurrency. Broker\Redis does not carry it and keeps its caps unchanged — Connection\Locking already serialises the coroutines sharing one connection. A Redis worker at job('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 only testStartRefusesConcurrencyOnAnExclusiveConsumer (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.

ServerJobsTest 11/11. Unit suite 49 tests, 4 errors, all Swoole\Coroutine\run() missing on the host in SwooleContextTest and present before this change. PHPStan and Pint clean, bin/monorepo validate passes.

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 maxCoroutines to 1 on a nats:// DSN and warns; this makes the library safe for callers that do not.

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.
…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.
@levivannoort
levivannoort merged commit eaeee09 into main Sep 9, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants