Skip to content

[WIP] fix(api_core): configure default max_metadata_size in gRPC helpers#17663

Draft
ohmayr wants to merge 1 commit into
mainfrom
pqc-google-api-core-grpc-metadata
Draft

[WIP] fix(api_core): configure default max_metadata_size in gRPC helpers#17663
ohmayr wants to merge 1 commit into
mainfrom
pqc-google-api-core-grpc-metadata

Conversation

@ohmayr

@ohmayr ohmayr commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕

@ohmayr ohmayr self-assigned this Jul 8, 2026

@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 configures a default grpc.max_metadata_size of 32768 when creating secure gRPC channels in both synchronous and asynchronous helpers, and adds corresponding tests. The reviewer noted that the default limit is currently only applied if custom options are already present in kwargs. To ensure the limit is always enforced, the reviewer suggested initializing options as an empty list when omitted, and provided updated test cases to verify both scenarios.

Comment on lines +378 to +387
if "options" in kwargs and isinstance(kwargs["options"], (list, tuple)):
opts = list(kwargs["options"])
if not any(
isinstance(opt, (list, tuple))
and len(opt) >= 2
and opt[0] == "grpc.max_metadata_size"
for opt in opts
):
opts.append(("grpc.max_metadata_size", 32768))
kwargs["options"] = opts

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.

high

The current implementation only configures grpc.max_metadata_size if options is already present in kwargs. This means that for the default case where no custom options are provided, the default max_metadata_size will not be set. To ensure that the default limit is always configured, we should initialize options as an empty list if it is not provided.

Suggested change
if "options" in kwargs and isinstance(kwargs["options"], (list, tuple)):
opts = list(kwargs["options"])
if not any(
isinstance(opt, (list, tuple))
and len(opt) >= 2
and opt[0] == "grpc.max_metadata_size"
for opt in opts
):
opts.append(("grpc.max_metadata_size", 32768))
kwargs["options"] = opts
options = kwargs.get("options")
if options is None:
options = []
elif isinstance(options, (list, tuple)):
options = list(options)
else:
options = None
if options is not None:
if not any(
isinstance(opt, (list, tuple))
and len(opt) >= 2
and opt[0] == "grpc.max_metadata_size"
for opt in options
):
options.append(("grpc.max_metadata_size", 32768))
kwargs["options"] = options

Comment on lines +307 to +316
if "options" in kwargs and isinstance(kwargs["options"], (list, tuple)):
opts = list(kwargs["options"])
if not any(
isinstance(opt, (list, tuple))
and len(opt) >= 2
and opt[0] == "grpc.max_metadata_size"
for opt in opts
):
opts.append(("grpc.max_metadata_size", 32768))
kwargs["options"] = opts

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.

high

The current implementation only configures grpc.max_metadata_size if options is already present in kwargs. This means that for the default case where no custom options are provided, the default max_metadata_size will not be set. To ensure that the default limit is always configured, we should initialize options as an empty list if it is not provided.

Suggested change
if "options" in kwargs and isinstance(kwargs["options"], (list, tuple)):
opts = list(kwargs["options"])
if not any(
isinstance(opt, (list, tuple))
and len(opt) >= 2
and opt[0] == "grpc.max_metadata_size"
for opt in opts
):
opts.append(("grpc.max_metadata_size", 32768))
kwargs["options"] = opts
options = kwargs.get("options")
if options is None:
options = []
elif isinstance(options, (list, tuple)):
options = list(options)
else:
options = None
if options is not None:
if not any(
isinstance(opt, (list, tuple))
and len(opt) >= 2
and opt[0] == "grpc.max_metadata_size"
for opt in options
):
options.append(("grpc.max_metadata_size", 32768))
kwargs["options"] = options

Comment on lines +735 to +739
def test_create_channel_max_metadata_size(grpc_secure_channel, auth_default):
options = [("grpc.max_send_message_length", -1)]
grpc_helpers_async.create_channel("example.com:443", options=options)
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]

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.

medium

Update the test to verify that grpc.max_metadata_size is correctly configured both when custom options are provided and when they are omitted.

Suggested change
def test_create_channel_max_metadata_size(grpc_secure_channel, auth_default):
options = [("grpc.max_send_message_length", -1)]
grpc_helpers_async.create_channel("example.com:443", options=options)
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]
def test_create_channel_max_metadata_size(grpc_secure_channel, auth_default):
options = [("grpc.max_send_message_length", -1)]
grpc_helpers_async.create_channel("example.com:443", options=options)
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]
grpc_secure_channel.reset_mock()
grpc_helpers_async.create_channel("example.com:443")
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]

Comment on lines +932 to +936
def test_create_channel_max_metadata_size(grpc_secure_channel, auth_default):
options = [("grpc.max_send_message_length", -1)]
grpc_helpers.create_channel("example.com:443", options=options)
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]

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.

medium

Update the test to verify that grpc.max_metadata_size is correctly configured both when custom options are provided and when they are omitted.

Suggested change
def test_create_channel_max_metadata_size(grpc_secure_channel, auth_default):
options = [("grpc.max_send_message_length", -1)]
grpc_helpers.create_channel("example.com:443", options=options)
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]
def test_create_channel_max_metadata_size(grpc_secure_channel, auth_default):
options = [("grpc.max_send_message_length", -1)]
grpc_helpers.create_channel("example.com:443", options=options)
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]
grpc_secure_channel.reset_mock()
grpc_helpers.create_channel("example.com:443")
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]

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.

1 participant