feat: handle SIGTERM in +watch and +subscribe for clean shutdown#549
feat: handle SIGTERM in +watch and +subscribe for clean shutdown#549romamo wants to merge 7 commits intogoogleworkspace:mainfrom
Conversation
Long-running pull loops in gmail +watch and events +subscribe now listen for SIGTERM alongside Ctrl+C. On receiving SIGTERM the loop acknowledges any in-flight messages and exits cleanly, enabling graceful shutdown in containers and process supervisors (Kubernetes, Cloud Run, systemd). Uses cfg'd let bindings to hoist the SIGTERM future outside tokio::select! branches, which do not support #[cfg] attributes.
Spawn each loop against a mock server returning empty pull responses, send SIGTERM from a side task after the first pull completes, and assert the loop returns Ok(()) within a 2-second timeout. Uses libc::raise(SIGTERM) (new dev-dep) + serial_test::serial to prevent cross-test signal interference.
🦋 Changeset detectedLatest commit: dc9b223 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds graceful shutdown handling for SIGTERM in the +watch and +subscribe commands, which is a great improvement for running the CLI in containerized environments. The implementation correctly uses #[cfg] attributes to provide this functionality on Unix systems while maintaining compatibility with other platforms.
My review includes a suggestion:
- Refactor the existing
SIGINT(ctrl_c) handling to be consistent with the newSIGTERMimplementation by creating the signal stream outside the main loop.
These changes will improve the code's consistency and maintainability.
Create a persistent SignalKind::interrupt() stream once before the loop instead of calling tokio::signal::ctrl_c() on every iteration. The SIGINT future is now prepared per-iteration with .recv() — the same pattern used for SIGTERM — keeping both signal handlers consistent and avoiding repeated handler re-registration.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces SIGTERM handling for graceful shutdown in the long-running +watch and +subscribe commands, which is a great improvement for containerized environments. The implementation correctly uses #[cfg(unix)] to maintain cross-platform compatibility and adds tests to verify the new signal handling. My review found a couple of critical issues in the new test assertions that would prevent the code from compiling. I've provided suggestions to fix them. Otherwise, the changes look good.
Use expect() to unwrap the timeout result with a message, then assert on the inner result — avoids the two-step is_ok()/unwrap() pattern flagged in review 3969752091.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request correctly adds SIGTERM handling for graceful shutdown, which is a valuable improvement for containerized environments. My review focuses on improving maintainability by refactoring duplicated code blocks found in both the application logic and the new test helpers. Addressing this duplication will make the code easier to maintain and reduce the risk of future bugs.
Extract `hoist_signals!` macro in both `watch.rs` and `subscribe.rs` to replace the repeated 4-line `#[cfg]` let-binding block that appeared twice per loop function. Output variable names are passed as macro arguments so they remain visible after the macro invocation (Rust macro hygiene). Move the identical `spawn_empty_pull_server` test helper from both files into `src/helpers/mod.rs` under `#[cfg(test)]` as a shared utility, and update both SIGTERM tests to reference `crate::helpers::test_helpers::spawn_empty_pull_server`.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request adds graceful shutdown handling for SIGTERM in the +watch and +subscribe commands, which is a great improvement for running the CLI in containerized environments. The implementation using tokio::signal and a macro to handle platform differences is well-thought-out. The addition of tests for signal handling is also excellent.
My main feedback is to address code duplication. The hoist_signals! macro is defined in two separate files. I've suggested moving it to a shared module to improve maintainability.
Define the macro once in the parent module before the `pub mod` declarations so Rust's textual scoping makes it available in all helper submodules (gmail::watch, events::subscribe) without an explicit import. Remove the per-file duplicate definitions.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces an important feature by adding SIGTERM handling for graceful shutdown, which is crucial for containerized environments. The approach of using tokio::signal is appropriate. However, I've found a critical issue with the implementation of the hoist_signals! macro that will cause compilation to fail on non-Unix platforms. I have provided detailed comments and suggestions to resolve this issue. Once these fixes are applied, the changes will be a solid improvement.
…all sites The macro referenced `sigint`/`sigterm` identifiers that only exist on Unix, so it would fail to compile on non-Unix targets. Gate the macro definition with `#[cfg(unix)]` (removing the internal cfg branches — the macro is now unix-only by contract) and pair every call site with a `#[cfg(not(unix))]` two-liner that creates the same `sigint_fut`/`sigterm_fut` bindings via `ctrl_c()` and `pending()`.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces graceful shutdown for long-running gws gmail +watch and gws events +subscribe processes by handling SIGTERM signals, in addition to existing Ctrl+C/SIGINT handling. The changes involve registering tokio::signal::unix::SignalKind::terminate() handlers, integrating them into tokio::select! loops using a new hoist_signals! macro to manage conditional compilation, and adding libc as a dev dependency for testing. New unit tests verify that both pull_loop and watch_pull_loop functions exit cleanly upon receiving SIGTERM.
Description
Long-running pull loops in
gws gmail +watchandgws events +subscribecurrently only handle Ctrl+C (SIGINT). SendingSIGTERM— the default signal used by Kubernetes, Cloud Run, Docker, and systemd to request graceful shutdown — leaves the process running until the next Pub/Sub poll timeout.This PR adds SIGTERM handling to both loops so the process exits cleanly on either signal.
Dry Run Output: N/A — no new commands or request changes; signal handling is runtime behaviour.
How it works
tokio::select!branches do not support#[cfg]attributes, so the SIGTERM future is hoisted into a cfg'dletbinding outside the macro. On non-Unix platforms the bindingresolves to
std::future::pending()(never fires), keeping Windows compatibility with zero overhead.The signal stream is created once before the loop so registration is not reset on each iteration.
Files changed
src/helpers/gmail/watch.rsselect!and sleepselect!src/helpers/events/subscribe.rspull_loopChecklist
AGENTS.mdguidelines (no generatedgoogle-*crates).cargo fmt --allto format the code perfectly.cargo clippy -- -D warningsand resolved all warnings.pnpx changeset) to document my changes.