Skip to content

Refactor AdePT into 2 libraries 4: Fix transport lifetime ownership and shutdown#521

Merged
agheata merged 1 commit intoapt-sim:masterfrom
SeverinDiederichs:clean_transport_lifetime
Mar 19, 2026
Merged

Refactor AdePT into 2 libraries 4: Fix transport lifetime ownership and shutdown#521
agheata merged 1 commit intoapt-sim:masterfrom
SeverinDiederichs:clean_transport_lifetime

Conversation

@SeverinDiederichs
Copy link
Collaborator

@SeverinDiederichs SeverinDiederichs commented Mar 19, 2026

This PR cleans up the lifetime of the shared AdePTTransport:

Instead of keeping the transport alive through a static owning shared_ptr, the static storage now only keeps a weak_ptr, while the real ownership remains with the AdePTTrackingManager instances.

This avoids destroying the transport very late during process teardown, where CUDA stream cleanup can already be in an invalid shutdown state.

Another explicit guard was added in the AsyncAdePTTransport:
In this wait loop for work of the G4 workers:

    while (gpuState.runTransport && std::none_of(eventStates.begin(), eventStates.end(), needTransport)) {
      using namespace std::chrono_literals;
      std::this_thread::sleep_for(10ms);
    }

The GPU steering thread can enter the sleep at shutdown, because the needTransport is already false, but the runTransport is not yet switched to false in the AsyncAdePTDestructor. If it is then switched to false by the worker in the shutdown, then one more iteration is run, while CUDA might already be in teardown, leading to a crash. This is prevented by a simple guard.

This PR is needed for upcoming restructuring, which exposed this life-time-management problem

It was verified that this PR

  • Changes physics results
  • Does not change physics results

@phsft-bot
Copy link

Can one of the admins verify this patch?

Copy link
Contributor

@agheata agheata left a comment

Choose a reason for hiding this comment

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

Looks good, LGTM

@agheata agheata merged commit a79bb71 into apt-sim:master Mar 19, 2026
4 checks passed
@SeverinDiederichs SeverinDiederichs deleted the clean_transport_lifetime branch March 19, 2026 08:13
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.

3 participants