Skip to content

Add clear_streams API for cleanup before exit#3395

Merged
zcbenz merged 1 commit intoml-explore:mainfrom
zcbenz:threading-atexit
Apr 14, 2026
Merged

Add clear_streams API for cleanup before exit#3395
zcbenz merged 1 commit intoml-explore:mainfrom
zcbenz:threading-atexit

Conversation

@zcbenz
Copy link
Copy Markdown
Collaborator

@zcbenz zcbenz commented Apr 11, 2026

Windows horror story: we should not do anything complicated during thread/process teardown (i.e. in the callbacks registered by system atexit explicitly via APIs or implicitly via C++ static/thread_local keywords), joining threads would cause deadlocks, and calling any CUDA API would fail.

The solution proposed here is to just provide an API to let users invoke before thread/process exit:

    std::thread t([]() {
      auto s = new_stream(default_device());
      eval(arange(10, s));
      clear_streams();
    });
    t.join();

The API call is optional, if users don't use it the program would still work normally on macOS/Linux that everything gets synchronized and cleared implicitly by us, except for Windows where the program would crash on exit, which is actually fine for most cases.

Copy link
Copy Markdown
Member

@angeloskath angeloskath left a comment

Choose a reason for hiding this comment

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

👍

@zcbenz zcbenz merged commit 435f0b6 into ml-explore:main Apr 14, 2026
16 checks passed
@zcbenz zcbenz deleted the threading-atexit branch April 14, 2026 09:41
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