Skip to content

feat: handle SIGTERM in +watch and +subscribe for clean shutdown#549

Open
romamo wants to merge 7 commits intogoogleworkspace:mainfrom
romamo:feat/sigterm-graceful-shutdown
Open

feat: handle SIGTERM in +watch and +subscribe for clean shutdown#549
romamo wants to merge 7 commits intogoogleworkspace:mainfrom
romamo:feat/sigterm-graceful-shutdown

Conversation

@romamo
Copy link

@romamo romamo commented Mar 18, 2026

Description

Long-running pull loops in gws gmail +watch and gws events +subscribe currently only handle Ctrl+C (SIGINT). Sending SIGTERM — 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'd let binding outside the macro. On non-Unix platforms the binding
resolves 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

File Change
src/helpers/gmail/watch.rs SIGTERM arm in pull select! and sleep select!
src/helpers/events/subscribe.rs Same pattern in pull_loop

Checklist

  • My code follows the AGENTS.md guidelines (no generated google-* crates).
  • I have run cargo fmt --all to format the code perfectly.
  • I have run cargo clippy -- -D warnings and resolved all warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have provided a Changeset file (e.g. via pnpx changeset) to document my changes.

romamo added 2 commits March 18, 2026 19:10
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-bot
Copy link

changeset-bot bot commented Mar 18, 2026

🦋 Changeset detected

Latest 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

@googleworkspace-bot googleworkspace-bot added area: distribution area: core Core CLI parsing, commands, error handling, utilities labels Mar 18, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 googleworkspace-cli application by introducing graceful shutdown capabilities for its long-running +watch and +subscribe commands. By responding to SIGTERM signals, the application can now terminate cleanly when managed by orchestrators or process supervisors, preventing abrupt interruptions and ensuring proper resource release. This change significantly improves the operational reliability of these services in production environments.

Highlights

  • Graceful Shutdown: Implemented SIGTERM signal handling in long-running gws gmail +watch and gws events +subscribe loops to allow for graceful shutdowns, particularly in containerized environments like Kubernetes, Cloud Run, and Docker.
  • Platform Compatibility: Ensured cross-platform compatibility by conditionally compiling SIGTERM handling only for Unix systems, using std::future::pending() for non-Unix platforms to avoid overhead.
  • Testing: Added new unit tests to verify that both pull_loop and watch_pull_loop correctly exit upon receiving a SIGTERM signal.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Generative AI Prohibited Use Policy, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 new SIGTERM implementation 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.
@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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`.
@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()`.
@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Core CLI parsing, commands, error handling, utilities area: distribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants