Skip to content

[Python] Resolve absl::InitializeLog warning#39779

Closed
sreenithi wants to merge 34 commits intogrpc:masterfrom
sreenithi:absl_init_log_python
Closed

[Python] Resolve absl::InitializeLog warning#39779
sreenithi wants to merge 34 commits intogrpc:masterfrom
sreenithi:absl_init_log_python

Conversation

@sreenithi
Copy link
Contributor

@sreenithi sreenithi commented Jun 6, 2025

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

@sreenithi sreenithi self-assigned this Jun 6, 2025
@sreenithi sreenithi added lang/Python release notes: yes Indicates if PR needs to be in release notes labels Jun 6, 2025
@sreenithi sreenithi marked this pull request as draft June 6, 2025 15:27
@sergiitk sergiitk self-requested a review June 19, 2025 05:53
# initialize gRPC
#
cdef _initialize():
InitializeLog()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was visible in the import cl internally.


include "_cygrpc/grpc.pxi"

# grpc-oss-only-begin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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", "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the implicit cast to a bool. Maybe something like this?

Suggested change
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:

bool gpr_parse_bool_value(const char* value, bool* dst) {
const char* kTrue[] = {"1", "t", "true", "y", "yes"};
const char* kFalse[] = {"0", "f", "false", "n", "no"};

Copy link
Member

@sergiitk sergiitk Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_set

Note: not tested and not sure this'll work as we want

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

sergiitk pushed a commit to grpc/proposal that referenced this pull request Jan 13, 2026
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need this comment anymore

@sergiitk
Copy link
Member

sergiitk commented Feb 3, 2026

Internal changes rolled back (ref cl/864955066), need to re-apply them again.

@sergiitk
Copy link
Member

sergiitk commented Feb 4, 2026

New internal change: cl/865415667

@ctiller ctiller closed this in b2925a0 Feb 4, 2026
@sergiitk sergiitk reopened this Feb 12, 2026
@sergiitk
Copy link
Member

sergiitk commented Feb 12, 2026

Reopening, as the change was rolled back in 1aefa48 (cl/865459236).

sreenithi added a commit to sreenithi/grpc that referenced this pull request Feb 16, 2026
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
sergiitk pushed a commit that referenced this pull request Feb 17, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gRPC Python logging "All log messages before absl::InitializeLog() is called are written to STDERR"

4 participants