Skip to content
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

Thread-safe client close #682

Merged

Conversation

randomir
Copy link
Member

@randomir randomir commented Jan 28, 2025

A follow-up to #680, in which we make sure client use is disabled also during closing, not just after the client has been closed.

We also make sure close() is thread-safe wrt attempted usage while closing.

TODO: consider converting Client.solvers_session to a ctxmgr, or making this session ephemeral in another way.
➡️ Keeping Client.solvers_session active during client lifespan, as it saves ~3ms on init due to disk cache access.

Copy link

codecov bot commented Jan 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.13%. Comparing base (59f8baa) to head (f506232).
Report is 8 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #682      +/-   ##
==========================================
+ Coverage   89.10%   89.13%   +0.02%     
==========================================
  Files          44       44              
  Lines        4846     4850       +4     
==========================================
+ Hits         4318     4323       +5     
+ Misses        528      527       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@randomir randomir requested a review from arcondello January 29, 2025 14:49
Copy link
Member

@arcondello arcondello left a comment

Choose a reason for hiding this comment

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

LGTM!

I suppose if you also wanted a context manager you could do something with a classmethod. Maybe

class Client:
    @contextlib.contextmanager
    @classmethod
    def as_ctx(cls, *args, **kwargs):
        client = cls(*args, **kwargs)
        yield client
        client.close()

might be less performant but convenient in some cases?

@randomir
Copy link
Member Author

@arcondello, Client already is a context manager (see __enter__ and __exit__ methods).

I considered converting solvers_session property to a context manager, but decided not to do it for now to save some init time.

@randomir randomir merged commit dabd71f into dwavesystems:master Jan 29, 2025
33 checks passed
@randomir randomir deleted the feature/thread-safe-client-close branch January 29, 2025 16:58
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.

2 participants