Skip to content

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

Merged
jpoehnelt merged 5 commits intomainfrom
feat/sigterm-shutdown
Mar 18, 2026
Merged

feat: handle SIGTERM in +watch and +subscribe for clean shutdown#550
jpoehnelt merged 5 commits intomainfrom
feat/sigterm-shutdown

Conversation

@jpoehnelt
Copy link
Member

Summary

Long-running pull loops (gws gmail +watch, gws events +subscribe) currently only handle Ctrl+C (SIGINT). Sending SIGTERM — the default signal used by Kubernetes, Cloud Run, Docker, and systemd — leaves the process running until the next poll timeout.

Changes

File Change
src/helpers/mod.rs New shutdown_signal() async helper — merges SIGINT + SIGTERM into one future
src/helpers/gmail/watch.rs Replace ctrl_c()
src/helpers/events/subscribe.rs Same pattern

Design

  • Shared helper: shutdown_signal() encapsulates the cfg-gated signal registration so callers just swap one call
  • No SIGINT refactor: Cross-platform ctrl_c() stays inside the helper — no behavior change on any platform
  • Non-Unix fallback: On Windows, only SIGINT is handled (via ctrl_c())
  • Minimal diff: 40 additions, 9 deletions across 3 source files

Checklist

  • Follows AGENTS.md guidelines
  • cargo fmt --all
  • cargo clippy -- -D warnings clean
  • 748 tests pass
  • Changeset provided

Add shared shutdown_signal() helper that merges SIGINT and SIGTERM
into a single future. Replace tokio::signal::ctrl_c() in both watch
and subscribe pull loops so they exit cleanly under Kubernetes,
Docker, and systemd.

On non-Unix platforms, only SIGINT (Ctrl+C) is handled.
@changeset-bot
Copy link

changeset-bot bot commented Mar 18, 2026

🦋 Changeset detected

Latest commit: 74e7bc5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@googleworkspace/cli Patch

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: core Core CLI parsing, commands, error handling, utilities and removed area: http 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 significantly improves the robustness of long-running gws commands by enabling them to respond to SIGTERM signals. This change is crucial for applications deployed in container orchestration systems like Kubernetes, Docker, or systemd, where SIGTERM is the standard signal for initiating a graceful shutdown. By centralizing the signal handling logic, the PR ensures consistent and clean termination behavior across relevant parts of the application.

Highlights

  • Enhanced Shutdown Handling: Implemented support for SIGTERM in addition to SIGINT for long-running processes like gws gmail +watch and gws events +subscribe, ensuring graceful exits in containerized environments.
  • Centralized Signal Logic: Introduced a new shutdown_signal() asynchronous helper in src/helpers/mod.rs to encapsulate cross-platform signal registration and handling.
  • Platform-Specific Implementation: Configured shutdown_signal() to handle both SIGINT and SIGTERM on Unix-like systems, while gracefully falling back to only SIGINT handling on non-Unix platforms.
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.

@googleworkspace-bot googleworkspace-bot added the cla: yes This human has signed the Contributor License Agreement. label Mar 18, 2026
@codecov
Copy link

codecov bot commented Mar 18, 2026

Codecov Report

❌ Patch coverage is 48.38710% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.04%. Comparing base (23ffc33) to head (74e7bc5).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/helpers/mod.rs 56.52% 10 Missing ⚠️
src/helpers/events/subscribe.rs 25.00% 3 Missing ⚠️
src/helpers/gmail/watch.rs 25.00% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #550   +/-   ##
=======================================
  Coverage   69.04%   69.04%           
=======================================
  Files          42       42           
  Lines       19261    19284   +23     
=======================================
+ Hits        13299    13315   +16     
- Misses       5962     5969    +7     

☔ View full report in Codecov by Sentry.
📢 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.

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 handling for SIGTERM to allow for graceful shutdown in containerized environments. The approach of creating a shared shutdown_signal helper is good. However, the current implementation of shutdown_signal has a critical race condition that could lead to unclean shutdowns, which defeats the purpose of the change. I've provided a detailed comment and a suggested fix to make the signal handling robust.

@github-actions github-actions bot added the gemini: reviewed Gemini Code Assist has reviewed the latest changes label Mar 18, 2026
Use OnceLock + tokio::sync::Notify so the signal handler stays active
for the process lifetime. Eliminates the race window between loop
iterations where a SIGTERM would bypass the handler.
@github-actions github-actions bot removed the gemini: reviewed Gemini Code Assist has reviewed the latest changes label Mar 18, 2026
@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 a shutdown_signal helper to handle both SIGINT and SIGTERM for graceful shutdown, which is a great improvement for containerized environments. The implementation is mostly solid, using a OnceLock and Notify pattern effectively. However, I've found a critical issue in the error handling within the new shutdown_signal function that could lead to a deadlock if registering the SIGTERM handler fails. My review includes a suggestion to fix this.

@github-actions github-actions bot added the gemini: reviewed Gemini Code Assist has reviewed the latest changes label Mar 18, 2026
Replace expect() with match: if signal(SIGTERM) fails, log a warning
and fall back to SIGINT-only. Prevents silent task death that would
hang all shutdown_signal() callers indefinitely.
@github-actions github-actions bot removed the gemini: reviewed Gemini Code Assist has reviewed the latest changes label Mar 18, 2026
@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 a helpful shutdown_signal helper to handle both SIGINT and SIGTERM for graceful shutdown, which is a great improvement for containerized environments. However, the implementation of shutdown_signal has a critical flaw that could cause the application to terminate unexpectedly on startup if it fails to register a signal handler. I've provided a detailed comment and a code suggestion to address this issue.

@github-actions github-actions bot added the gemini: reviewed Gemini Code Assist has reviewed the latest changes label Mar 18, 2026
Use Ok(_) pattern matching in select! branches and expect() for
standalone ctrl_c().await calls. Previously .ok() silently swallowed
errors, causing notify_waiters() to fire immediately.
@github-actions github-actions bot removed the gemini: reviewed Gemini Code Assist has reviewed the latest changes label Mar 18, 2026
@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 a clean way to handle both SIGINT and SIGTERM for graceful shutdown, which is a great improvement for containerized environments. The implementation uses a shutdown_signal helper that correctly encapsulates the logic. I found one high-severity issue in the new helper where a failure to register the SIGINT handler would be silently ignored, making the application unresponsive to Ctrl+C. I've provided a suggestion to fix this by explicitly handling the result and panicking on error, consistent with other parts of the code.

@github-actions github-actions bot added the gemini: reviewed Gemini Code Assist has reviewed the latest changes label Mar 18, 2026
Bind the full Result from ctrl_c() and expect() on it instead of
pattern matching Ok(_), which silently dropped the branch on Err.
@github-actions github-actions bot removed the gemini: reviewed Gemini Code Assist has reviewed the latest changes label Mar 18, 2026
@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 a helpful shutdown_signal utility to handle both SIGINT and SIGTERM for graceful shutdowns, which is a great improvement for containerized environments. The implementation, however, contains a critical issue where a failure to register the SIGINT handler can cause the background signal-watching task to panic. This leads to a deadlock in any part of the application awaiting a shutdown signal, making the process hang. I've provided a detailed comment and a suggested fix in src/helpers/mod.rs to address this by handling errors gracefully instead of panicking.

@github-actions github-actions bot added the gemini: reviewed Gemini Code Assist has reviewed the latest changes label Mar 18, 2026
@jpoehnelt jpoehnelt merged commit a87037b into main Mar 18, 2026
33 of 34 checks passed
@jpoehnelt jpoehnelt deleted the feat/sigterm-shutdown branch March 18, 2026 20:42
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 cla: yes This human has signed the Contributor License Agreement. gemini: reviewed Gemini Code Assist has reviewed the latest changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants