Repository navigation
fix(grpc): silence fork noise after early grpc import and remaining forks - #249
Conversation
There was a problem hiding this comment.
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.
…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.
b0df2ca to
e75dc69
Compare
Stacked on #248.
Problem
PR #217 defaulted
GRPC_VERBOSITY=ERRORat package import time only when"grpc" not in sys.modules. In practice, user notebooks commonly importgoogle.cloud.dataproc_v1first (for examplefrom google.cloud.dataproc_v1 import Sessionfollowed byfrom google.cloud.managed_spark_connect import ManagedSparkSession), which importsgrpcand populates gRPC C-coreConfigVarsbefore this package is imported. BecauseGRPC_VERBOSITYremained unset, macOS users still sawINFO-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()calledsubprocess.run(["ps", ...])withclose_fds=True, causing Python on macOS tofork()instead of usingposix_spawn()._create_dataproc_client()passed unrefreshed ADC credentials, allowing gRPCAuthMetadataPluginto refresh them (forking external credential helpers) on a background thread during an RPC.Summary
GRPC_VERBOSITY=ERRORafter earlygrpcimports (__init__.py,README.md): WhenGRPC_VERBOSITYis unset andgrpcwas already imported before any sync channel/server/call-credentials module (grpc._channel,grpc._server,grpc._auth), shutdown handler,grpc.aioPollerCompletionQueuethread, or Linux/proc/self/task/*/commC-core thread (grpc*,event_engine,lifeguard) has started, setGRPC_VERBOSITY=ERRORand callcygrpc.reset_grpc_config_vars()so the firstgrpc_init()appliesERROR. ExplicitGRPC_VERBOSITYvalues always win, andConfigVarsis never reset once gRPC C-core may already be running.posix_spawn()in macOSpsIDE detection (ide.py): Resolvepsto an absolute path viashutil.which("ps")+os.path.isabs()and passexecutableandclose_fds=Falseon POSIX so Python usesos.posix_spawn()(skippingpthread_atfork()handlers)._create_dataproc_client()(session.py): Best-effort refresh cached ADC synchronously outside_CLIENT_CACHE_LOCKbefore opening a gRPC channel and on cache hits whose token has expired, preventingAuthMetadataPluginfrom invoking a credential refresh on a background thread during an active RPC.tests/unit/test_grpc_fork_noise.py,tests/unit/test_ide.py,tests/unit/test_dataproc.py): Added regression tests for earlydataproc_v1imports (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/procthread detection),posix_spawn/ absolute-path guard in_ps_proc_lookup(), and Dataproc client pre-warming on initial creation and expired cache hits.Testing
pyink --checkon modified Python files: passedpytest -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