Repository navigation
fix(subscriber): make streaming pull responsive to SIGTERM - #17
Merged
Merged
Conversation
Merged
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.
Description
Two small fixes to make the Pub/Sub subscriber responsive to SIGTERM in containerized environments.
Motivation
In K8s/Docker, SIGTERM is followed by SIGKILL after
terminationGracePeriodSeconds(default 30s). Two paths in the subscriber blocked the shutdown signal frombeing acted on in time:
StreamingSubscriber::stream()callspull(['returnImmediately' => false, ...]), which blocks on a gRPC streaming pull until messages arrive or theserver-side deadline elapses (often 30–90s). The trait's synchronous
pcntl_signal_dispatch()polling could not interrupt this — SIGTERM was only noticed afterpull()returned, by which time SIGKILL may have already fired.shouldStop(). With slow handlers and a full batch (up to 100messages by default in streaming mode), shutdown could be delayed significantly even when the signal was noticed promptly.
Changes
src/Concerns/HandlesSignals.php— callpcntl_async_signals(true)inregisterSignalHandlers()so SIGTERM/SIGINT/SIGQUIT handlers fire during theblocking pull syscall and set
$shouldStopFlagimmediately. The existingpcntl_signal_dispatch()calls remain (they become no-ops under async mode but areharmless).
src/Subscriber/Subscriber.php— addif ($this->shouldStop()) break;after each message inpull()'s loop.src/Subscriber/StreamingSubscriber.php— same per-message check instream()'s inner loop.A follow-up PR will address explicit nack-on-shutdown of unprocessed pulled messages (currently they're abandoned and redelivered after the ack-deadline
expires).
Fixes # (issue)
Type of change
How Has This Been Tested?
Full Pest suite (
composer test): 233 passed, 1 skipped, 415 assertions.Updated three streaming-subscriber tests whose stop-after-N-pulls plumbing relied on counting
shouldStop()calls — the new inner-loop check increments thatcounter more often, so the threshold bumped from
>= 2to>= 3with a comment explaining the call cadence. No test removed; behavioral expectations (pullcalled twice, ack/nack expectations) are unchanged.
Manual verification of K8s-style shutdown requires a deployed environment; not covered by the unit tests.
composer testpassescomposer analyse(PHPStan / Larastan) passescomposer pintshows no formatting changesAdded or updated tests covering the change
Test Configuration:
Type of change
How Has This Been Tested?
Updated three streaming-subscriber tests whose stop-after-N-pulls plumbing relied on counting
shouldStop()calls — the new inner-loop check increments thatcounter more often, so the threshold bumped from
>= 2to>= 3with a comment explaining the call cadence. No test removed; behavioral expectations (pullcalled twice, ack/nack expectations) are unchanged.
Manual verification of K8s-style shutdown requires a deployed environment; not covered by the unit tests.
composer testpassescomposer analyse(PHPStan / Larastan) passescomposer pintshows no formatting changesAdded or updated tests covering the change
Test Configuration:
Checklist
Type of change
How Has This Been Tested?
Full Pest suite (
composer test): 233 passed, 1 skipped, 415 assertions.Updated three streaming-subscriber tests whose stop-after-N-pulls plumbing relied on counting
shouldStop()calls — the new inner-loop check increments thatcounter more often, so the threshold bumped from
>= 2to>= 3with a comment explaining the call cadence. No test removed; behavioral expectations (pullcalled twice, ack/nack expectations) are unchanged.
Manual verification of K8s-style shutdown requires a deployed environment; not covered by the unit tests.
composer testpassescomposer analyse(PHPStan / Larastan) passescomposer pintshows no formatting changesTest Configuration:
Checklist