Skip to content

fix: add context manager and finalizer to JsonlLogger#30

Open
haosenwang1018 wants to merge 1 commit intogensyn-ai:mainfrom
haosenwang1018:fix/jsonl-logger-context-manager
Open

fix: add context manager and finalizer to JsonlLogger#30
haosenwang1018 wants to merge 1 commit intogensyn-ai:mainfrom
haosenwang1018:fix/jsonl-logger-context-manager

Conversation

@haosenwang1018
Copy link

Problem

JsonlLogger opens a file handle in __init__ but relies on explicit close() calls. If an exception occurs before close() is called, the file descriptor leaks and buffered data may be lost.

Fix

Add __enter__/__exit__ methods so JsonlLogger can be used as a context manager (with JsonlLogger(...) as logger:), and a __del__ finalizer as a safety net during garbage collection.

This is backward-compatible — existing code that calls close() directly continues to work.

JsonlLogger opens a file handle in __init__ but has no __del__ or
context manager protocol, so the handle leaks if close() is not
explicitly called (e.g., on exception).

Add __enter__/__exit__ for use as context manager and __del__ as
safety net for garbage collection.
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