Skip to content

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

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

feat: handle SIGTERM in +watch and +subscribe for clean shutdown#549
romamo wants to merge 4 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.

@changeset-bot
Copy link

changeset-bot bot commented Mar 18, 2026

⚠️ No Changeset found

Latest commit: a89629a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a 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.

@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.

@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.

@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.

@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.

@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.

romamo added 4 commits March 20, 2026 09:47
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.
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.
shutdown_signal() uses OnceLock + notify_waiters() which fires once per
process. Running SIGTERM tests in two modules (events::subscribe runs
first alphabetically) exhausts the singleton before the watch test runs,
causing it to time out. Since both loops share the same shutdown_signal()
implementation, one test covers the mechanism.
…ntimes

tokio::spawn ties the background task to a specific tokio runtime.
In tests, each #[tokio::test] creates its own runtime; when it drops,
the spawned signal-handler task is cancelled. Subsequent tests find the
OnceLock already initialised but the handler dead, so notify_waiters()
is never called and the SIGTERM test times out.

Replace tokio::spawn with std::thread::spawn + a dedicated
current-thread runtime. The thread (and its runtime) live for the
whole process lifetime regardless of which test runtime initialised
the OnceLock, making the SIGTERM test reliable.
@romamo romamo force-pushed the feat/sigterm-graceful-shutdown branch from dc9b223 to a89629a Compare March 20, 2026 07:56
@googleworkspace-bot
Copy link
Collaborator

/gemini review

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