Skip to content

refactor(tos): add context manager support and proper resource cleanup for TOSService - #164

Open
zhangning-agent wants to merge 2 commits into
volcengine:mainfrom
zhangning-agent:fix-tos-close-issue
Open

refactor(tos): add context manager support and proper resource cleanup for TOSService#164
zhangning-agent wants to merge 2 commits into
volcengine:mainfrom
zhangning-agent:fix-tos-close-issue

Conversation

@zhangning-agent

Copy link
Copy Markdown

Description

This PR adds context manager (__enter__ / __exit__) support and a close() method to TOSService, ensuring underlying TOS client connections and sockets are properly released upon exit.

Key Changes

  1. TOSService Improvements:
    • Added close() method with idempotency protection (self.client = None).
    • Implemented __enter__ and __exit__ context manager protocol.
    • Removed implicit __del__ finalizer in favor of deterministic resource management.
  2. VeCPCRBuilder Usage:
    • Updated VeCPCRBuilder._upload_to_tos to instantiate TOSService via with TOSService(...) as tos_service: block.
  3. Tests:
    • Added unit test tests/toolkit/volcengine/test_tos_service.py to verify close() and context manager behavior.
    • Updated tests/toolkit/builders/test_ve_pipeline_upload_tos.py to assert close() is called on exit.

Testing

Ran pytest suite locally:

  • tests/toolkit/builders/test_ve_pipeline_upload_tos.py
  • tests/toolkit/volcengine/test_tos_service.py

Result: 12 passed cleanly.

1. add close() method to TOSService to release client connections
2. implement __enter__ and __exit__ for context manager usage
3. update VeCPCRBuilder to use context manager for TOSService and add finally block for cleanup
4. add unit tests for TOSService resource management
5. update existing tests to verify close() method is called
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