Skip to content

pubsub: nack unreturned messages on Subscription.Shutdown - #3848

Merged
vangent merged 1 commit into
google:masterfrom
iamabhilaksh:pubsub-shutdown-nack
Nov 22, 2026
Merged

vangent merged 1 commit into
google:masterfrom
iamabhilaksh:pubsub-shutdown-nack

Conversation

@iamabhilaksh

@iamabhilaksh iamabhilaksh commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #3845, as discussed there.

Problem

Subscription pulls messages ahead of time, before Receive returns them. If Shutdown runs while some of those messages are still waiting, nothing acks or nacks them. The service keeps them reserved for this subscription until the timeout runs out (the ack deadline on GCP, the visibility timeout on SQS, the lock on Service Bus), so other consumers can't get them for up to about a minute.

This covers two kinds of messages:

  • messages already pulled and waiting for Receive
  • messages from pulls still in progress when Shutdown is called

Change

For drivers that support nack, Shutdown now:

  1. cancels the pulls still in progress
  2. waits for them to return (bounded by the Shutdown context; drivers are expected to return within about a second)
  3. nacks every message that wasn't returned, before the ack batcher shuts down, so they're available again right away

Drivers that can't nack (kafka, nats) behave as before. Nothing is nacked twice: gcppubsubv2's Close from #3845 skips messages that were already acked or nacked.

Testing

TestShutdownNacksPrefetched covers waiting messages, a pull in progress at Shutdown, and a driver that can't nack. Both nack cases fail on master. go test -race ./pubsub/... passes, as do kafkapubsub and rabbitpubsub against local brokers, and natspubsub.

End to end: 8 workers receive and ack about half of 300 messages, then Shutdown is called. A second subscriber measures how long the rest take to come back. The timeout was 60s everywhere.

  • Azure Service Bus emulator: on master, messages waited the full 60s in 6 of 7 runs. With this change, 0 of 6; everything came back in under 1s.
  • GCP Pub/Sub emulator, gcppubsub: on master, 67 to 76 messages per run waited 60s (3 of 3 runs). With this change, none did.
  • GCP Pub/Sub emulator, gcppubsubv2: pubsub/gcppubsubv2: nack buffered messages in Close #3845 already handles this on master. With this change the nacks just go out earlier, and redelivery was under 0.1s with no duplicates. Both builds occasionally held a batch for 60s (2 and 3 of 29 runs). That comes from the client library and happens on master too.
  • Real GCP Pub/Sub, v1 and v2: no regressions. One v2 run nacked 125 messages through this path before Close ran; all came back within about 16s, with no duplicates or errors.
  • Real AWS SQS, 6 runs each: within 80s, 38 of 857 messages came back on master, and 533 of 843 with this change (334 in under 5s). Most of the rest were probably from canceled pulls that SQS had already handed out, which stay hidden until the timeout. Shutdown took under 0.5s either way.
  • RabbitMQ: no change. Its Close doesn't cancel the consumer, which is a separate issue I'll file.

Shutdown now cancels in-flight ReceiveBatch calls, waits for them to finish (bounded by the Shutdown ctx), and nacks every message Receive never returned through the ack batcher, so they are redelivered without waiting for the ack deadline to expire. Drivers that can't nack are unchanged.
@iamabhilaksh
iamabhilaksh marked this pull request as ready for review November 16, 2026 12:18
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 81.54%. Comparing base (3133066) to head (f7ea52c).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
pubsub/pubsub.go 93.75% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3848      +/-   ##
==========================================
+ Coverage   81.52%   81.54%   +0.01%     
==========================================
  Files         100      100              
  Lines       12241    12256      +15     
==========================================
+ Hits         9980     9994      +14     
- Misses       2260     2261       +1     
  Partials        1        1              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vangent
vangent merged commit 81f91c2 into google:master Nov 22, 2026
13 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