[Python] Resolve absl::InitializeLog warning#39779
[Python] Resolve absl::InitializeLog warning#39779sreenithi wants to merge 34 commits intogrpc:masterfrom
Conversation
b9e6de8 to
e153e5b
Compare
| # initialize gRPC | ||
| # | ||
| cdef _initialize(): | ||
| InitializeLog() |
There was a problem hiding this comment.
Do we know that _initialize is called exactly once even if client code imports grpc multiple times? If not, we should have an internal state to check if we have already initialized it.
| if not _disable_absl_init_log: | ||
| InitializeLog() | ||
| # grpc-oss-only-end | ||
|
|
There was a problem hiding this comment.
This was visible in the import cl internally.
|
|
||
| include "_cygrpc/grpc.pxi" | ||
|
|
||
| # grpc-oss-only-begin |
There was a problem hiding this comment.
We're already stripping unnecessary code from absl.pxi, no need to exclude it here too
| include "_cygrpc/aio/server.pyx.pxi" | ||
|
|
||
| # grpc-oss-only-begin | ||
| cdef bint _disable_absl_init_log = os.environ.get("GRPC_PYTHON_DISABLE_ABSL_INIT_LOG", "") |
There was a problem hiding this comment.
I don't like the implicit cast to a bool. Maybe something like this?
| cdef bint _disable_absl_init_log = os.environ.get("GRPC_PYTHON_DISABLE_ABSL_INIT_LOG", "") | |
| cdef bint _disable_absl_init_log = os.environ.get("GRPC_PYTHON_DISABLE_ABSL_INIT_LOG", "") in {"1", "t", "true", "y", "yes"} |
I got the set from gpr_parse_bool_value:
Lines 323 to 325 in ae8671e
There was a problem hiding this comment.
I'm also ok with using this method directly, but it'll be a bit more complex. Something like
cdef bint _absl_init_log_env_is_set = 0
cdef bint _absl_init_log_env_is_parsed = gpr_parse_bool_value(os.environ.get("GRPC_PYTHON_DISABLE_ABSL_INIT_LOG", "").encode(), &_absl_init_log_env_is_set)
cdef bint _disable_absl_init_log = _absl_init_log_env_is_parsed && _absl_init_log_env_is_setNote: not tested and not sure this'll work as we want
There was a problem hiding this comment.
Though eventually it may not be a bad idea to add a wrapper around this, something like _parse_truthy_env(env_var_name: str) -> bool.
Proposes changes to Abseil logging initialization in gRPC Python to resolve the issue the absl warning `All log messages before absl::InitializeLog() is called are written to STDERR`. Related: - Bug report: grpc/grpc#38703 - Implementation PoC: grpc/grpc#39779 - b/423754102
| include "_cygrpc/aio/channel.pyx.pxi" | ||
| include "_cygrpc/aio/server.pyx.pxi" | ||
|
|
||
| # Include only for OSS |
There was a problem hiding this comment.
Not sure we need this comment anymore
|
Internal changes rolled back (ref cl/864955066), need to re-apply them again. |
|
New internal change: cl/865415667 |
|
Reopening, as the change was rolled back in 1aefa48 (cl/865459236). |
Fixes grpc#38703 After Core recently changed its logging system to use absl logging, gRPC python users started seeing the warning log `WARNING: All log messages before absl::InitializeLog() is called are written to STDERR.` This issue especially affects users who are indirect users of the gRPC Python library too, such as Gemini API users. `absl::InitializeLog()` is a C++ library that cannot directly be called in the Python layer. It is hence added in the Cython layer at the time of initializing. The Python layer so far did not have a dependency on the absl library, hence this PR also adds an external dependency on `absl/log:initialize` Design gRFC for this: grpc/proposal#505 Closes grpc#39779 COPYBARA_INTEGRATE_REVIEW=grpc#39779 from sreenithi:absl_init_log_python 59eb1e5 PiperOrigin-RevId: 869586957
Backport of #39779 to v1.78.x. --- Fixes #38703 After Core recently changed its logging system to use absl logging, gRPC python users started seeing the warning log `WARNING: All log messages before absl::InitializeLog() is called are written to STDERR.` This issue especially affects users who are indirect users of the gRPC Python library too, such as Gemini API users. `absl::InitializeLog()` is a C++ library that cannot directly be called in the Python layer. It is hence added in the Cython layer at the time of initializing. The Python layer so far did not have a dependency on the absl library, hence this PR also adds an external dependency on `absl/log:initialize` Design gRFC for this: grpc/proposal#505 However, abseil-cpp currently [doesn't allow `absl::InitializeLog()` to be called more than once](abseil/abseil-cpp#1656), and will result in an error like: ``` [globals.cc : 104] RAW: absl::log_internal::SetTimeZone() has already been called ``` The changes in this PR may hence cause problems to a small percentage of users with a custom build that's linking directly against gRPC Cython code, and then call absl::InitializeLog() (directly or transitively). To solve this problem, this PR also introduces a new environment variable `GRPC_PYTHON_DISABLE_ABSL_INIT_LOG` that will allow users to opt-out of this automatic abseil log initialization when set.
Fixes #38703
After Core recently changed its logging system to use absl logging, gRPC python users started seeing the warning log
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR.This issue especially affects users who are indirect users of the gRPC Python library too, such as Gemini API users.
absl::InitializeLog()is a C++ library that cannot directly be called in the Python layer. It is hence added in the Cython layer at the time of initializing.The Python layer so far did not have a dependency on the absl library, hence this PR also adds an external dependency on
absl/log:initializeDesign gRFC for this: grpc/proposal#505
However, abseil-cpp currently doesn't allow
absl::InitializeLog()to be called more than once, and will result in an error like:The changes in this PR may hence cause problems to a small percentage of users with a custom build that's linking directly against gRPC Cython code, and then call absl::InitializeLog() (directly or transitively).
To solve this problem, this PR also introduces a new environment variable
GRPC_PYTHON_DISABLE_ABSL_INIT_LOGthat will allow users to opt-out of this automatic abseil log initialization when set.