Skip to content

Fix: dictionary changed size during iteration in GCSObjectMetadataClient #468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions gokart/gcs_obj_metadata_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,14 @@ def _get_label_size(label_name: str, label_value: str) -> int:
)
if current_total_metadata_size <= max_gcs_metadata_size:
return labels
for label_name, label_value in reversed(labels.items()):
to_remove = []
for label_name, label_value in reversed(list(labels.items())):
size = _get_label_size(label_name, label_value)
del labels[label_name]
to_remove.append(label_name)
current_total_metadata_size -= size
if current_total_metadata_size <= max_gcs_metadata_size:
break

for key in to_remove:
del labels[key]
return labels
10 changes: 10 additions & 0 deletions test/test_gcs_obj_metadata_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ def test_get_patched_obj_metadata_with_nested_required_task_outputs(self):
got['__required_task_outputs'], '{"nested_task": {"nest": {"__gokart_task_name": "task1", "__gokart_output_path": "path/to/output1"}}}'
)

def test_adjust_gcs_metadata_limit_size_runtime_error(self):
large_labels = {}
for i in range(100):
large_labels[f'key_{i}'] = 'x' * 1000

result = GCSObjectMetadataClient._adjust_gcs_metadata_limit_size(large_labels)

total_size = sum(len(k.encode('utf-8')) + len(v.encode('utf-8')) for k, v in result.items())
Copy link
Member

Choose a reason for hiding this comment

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

Is encode('utf-8') needs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When working with non-ASCII characters (such as Japanese etc.), it is necessary because the number of characters and the number of bytes do not match.

Copy link
Member

Choose a reason for hiding this comment

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

I think so. But, in this test case, is it need?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I apologize. I misunderstood it as production code. Since this test only needs to verify that no error occurs, it doesn't seem necessary.

self.assertLessEqual(total_size, 8 * 1024)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you define a constant value in gokart/gcs_obj_metadata_client.py

MAX_GCS_METADATA_SIZE: Final[int] = 8 * 1024

and refer here and L157 of gokart/gcs_obj_metadata_client.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your review. I've fixed it!



class TestGokartTask(unittest.TestCase):
@patch.object(_DummyTaskOnKart, '_get_output_target')
Expand Down