improve synchronization in context manager