Skip to content

fix(grpc): silence fork noise after early grpc import and remaining forks - #249

Merged
dborowitz merged 1 commit into
dborowitz-adc-cache-invalidationfrom
dborowitz-grpc-fork-noise
Dec 4, 2026
Merged

dborowitz merged 1 commit into
dborowitz-adc-cache-invalidationfrom
dborowitz-grpc-fork-noise

Conversation

@dborowitz

@dborowitz dborowitz commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #248.

Problem

PR #217 defaulted GRPC_VERBOSITY=ERROR at package import time only when "grpc" not in sys.modules. In practice, user notebooks commonly import google.cloud.dataproc_v1 first (for example from google.cloud.dataproc_v1 import Session followed by from google.cloud.managed_spark_connect import ManagedSparkSession), which imports grpc and populates gRPC C-core ConfigVars before this package is imported. Because GRPC_VERBOSITY remained unset, macOS users still saw INFO-level gRPC fork-handler output (fork_posix.cc:71 / ev_poll_posix.cc) during session creation.

In addition, two post-gRPC fork() sites remained during session creation:

  • ide._ps_proc_lookup() called subprocess.run(["ps", ...]) with close_fds=True, causing Python on macOS to fork() instead of using posix_spawn().
  • _create_dataproc_client() passed unrefreshed ADC credentials, allowing gRPC AuthMetadataPlugin to refresh them (forking external credential helpers) on a background thread during an RPC.

Summary

  1. Default GRPC_VERBOSITY=ERROR after early grpc imports (__init__.py, README.md): When GRPC_VERBOSITY is unset and grpc was already imported before any sync channel/server/call-credentials module (grpc._channel, grpc._server, grpc._auth), shutdown handler, grpc.aio PollerCompletionQueue thread, or Linux /proc/self/task/*/comm C-core thread (grpc*, event_engine, lifeguard) has started, set GRPC_VERBOSITY=ERROR and call cygrpc.reset_grpc_config_vars() so the first grpc_init() applies ERROR. Explicit GRPC_VERBOSITY values always win, and ConfigVars is never reset once gRPC C-core may already be running.
  2. Use posix_spawn() in macOS ps IDE detection (ide.py): Resolve ps to an absolute path via shutil.which("ps") + os.path.isabs() and pass executable and close_fds=False on POSIX so Python uses os.posix_spawn() (skipping pthread_atfork() handlers).
  3. Synchronously pre-warm ADC in _create_dataproc_client() (session.py): Best-effort refresh cached ADC synchronously outside _CLIENT_CACHE_LOCK before opening a gRPC channel and on cache hits whose token has expired, preventing AuthMetadataPlugin from invoking a credential refresh on a background thread during an active RPC.
  4. Tests (tests/unit/test_grpc_fork_noise.py, tests/unit/test_ide.py, tests/unit/test_dataproc.py): Added regression tests for early dataproc_v1 imports (including live POSIX fork-noise suppression), runtime-started safety guards (sync channel, grpc.aio, access_token_call_credentials, ssl_server_credentials, local_server_credentials, Linux /proc thread detection), posix_spawn / absolute-path guard in _ps_proc_lookup(), and Dataproc client pre-warming on initial creation and expired cache hits.

Testing

  • pyink --check on modified Python files: passed
  • pytest -q -p no:cacheprovider -n 8 tests/unit/test_dataproc.py tests/unit/test_grpc_fork_noise.py tests/unit/test_session_templates.py tests/unit/test_ide.py: 209 passed, 17 skipped, 25 subtests passed

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

Copy link
Copy Markdown
Contributor

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 implements several improvements to suppress gRPC fork-handler noise and prevent background credential refreshes from conflicting with active gRPC execution contexts. Specifically, it updates the package initialization to safely configure default GRPC_VERBOSITY to ERROR even when other Google Cloud packages are imported first, and modifies process lookups on POSIX to use os.posix_spawn instead of fork(). Additionally, it introduces synchronous credential pre-warming before creating Dataproc control-plane clients to avoid background refreshes during active RPCs, and unifies client creation and caching for both session and template controllers. Comprehensive unit tests have been added to verify these behaviors. No review comments were provided, so there is no additional feedback to address.

@dborowitz
dborowitz added this pull request to stack #250 December 4, 2026 04:59
@ptch314
ptch314 self-requested a review December 4, 2026 15:12
…orks

PR #217 defaulted GRPC_VERBOSITY=ERROR at package import time only when
`grpc` was not yet in `sys.modules`. However, common user code imports
`google.cloud.dataproc_v1` (e.g. `from google.cloud.dataproc_v1 import
Session`) ahead of `google.cloud.managed_spark_connect`, which eagerly
imports `grpc` and caches `ConfigVars` before our package runs, leaving
gRPC INFO fork-handler logs (`fork_posix.cc` / `ev_poll_posix.cc`)
enabled on macOS.

Three complementary fixes:
1. Reset gRPC `ConfigVars` before C-core initializes (`__init__.py`): when
   `GRPC_VERBOSITY` is unset and `grpc` is already imported but no sync
   channel/server/call-credentials module, shutdown handler, `grpc.aio`
   `PollerCompletionQueue` thread, or Linux C-core background thread
   (`grpc*` / `event_engine` / `lifeguard`) has started yet, set
   `GRPC_VERBOSITY=ERROR` and call `cygrpc.reset_grpc_config_vars()` so
   the first `grpc_init()` picks up `ERROR`. Explicit `GRPC_VERBOSITY`
   values still win, and `ConfigVars` is never reset once gRPC C-core may
   be running.
2. Use `posix_spawn()` in macOS `ps` IDE detection (`ide.py`): resolve
   `ps` to an absolute path with `shutil.which("ps")` and pass
   `executable` + `close_fds=False` on POSIX so `_ps_proc_lookup()` avoids
   `fork()` (and gRPC `pthread_atfork()` handlers) when called right after
   `create_session()`.
3. Pre-warm ADC in `_create_dataproc_client()` (`session.py`): best-effort
   refresh cached ADC synchronously outside `_CLIENT_CACHE_LOCK` before
   opening a gRPC channel and on cache hits whose token has expired, preventing
   `AuthMetadataPlugin` from invoking a credential refresh on a background
   thread during an active RPC.
@dborowitz
dborowitz force-pushed the dborowitz-grpc-fork-noise branch from b0df2ca to e75dc69 Compare December 4, 2026 21:03
@dborowitz
dborowitz merged commit a446c74 into main Dec 4, 2026
37 of 57 checks passed
@dborowitz
dborowitz deleted the dborowitz-grpc-fork-noise branch December 4, 2026 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants