﻿# PyTorch process-group shutdown: do not leave communicator order to garbage collection

Coordinate process-group teardown in every PyTorch trainer process, separating completed model work from collective communicator cleanup and reinitialization.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/pytorch-process-group-destruction-order/
Language: en

A distributed job writes its final checkpoint and then hangs while exiting. Operators investigate the last training collective, although application work already finished. Communication resources also have a distributed lifecycle: leaving their destruction to unrelated garbage-collection schedules can create a different exit-time ordering problem.

## Separate useful completion from teardown

Record the last required computation, artifact publication, outstanding communication and process-group cleanup separately. A final loss line does not establish that all ranks entered teardown. The [NCCL progress guide](/journal/guide-perf-nccl-ras-count-skew/) explains why communicator-specific observations matter. At exit, preserve which group and process remain alive rather than comparing an active group with another already finalized by its members. Capture exceptions that may have bypassed the intended cleanup path on one rank.

## Follow the per-trainer contract

The [PyTorch distributed reference](https://docs.pytorch.org/docs/2.15/distributed.html) recommends explicit destroy_process_group when communication is no longer needed. It specifies calling from each trainer process rather than only the outer launcher. Review the application lifecycle accordingly. A parent process cannot demonstrate that every distributed child executed its required cleanup merely by returning from a launcher function. Keep successful cleanup distinguishable from a worker killed after a timeout or a process that exited before reaching the common boundary.

## Avoid nondeterministic destruction order

PyTorch explains that ProcessGroupNCCL destruction invokes ncclCommAbort, whose collective requirements can conflict with nondeterministic Python garbage-collection order, especially with several groups. The [NCCL communicator guide](https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/usage/communicators.html) supplies the underlying lifecycle context. Do not treat deleting a local Python variable as a coordinated global release. Preserve a consistent application cleanup plan across ranks rather than relying on incidental reference lifetime or assuming the last surviving group can safely finalize in any order.

## Finish asynchronous dependencies deliberately

Review outstanding communication and application threads before teardown. The distributed reference discusses consistent ordering and work synchronization when using multiple groups. A cleanup barrier added after groups are already destroyed is not a valid universal repair. The [asynchronous ownership guide](/journal/learn-async-shared-copy-pipeline/) explains the adjacent need to separate submitted work from completed use. Identify a supported point where required communications are complete and no helper will initiate another operation on a group being removed.

## Exercise ordinary and exceptional exits

In a disposable multi-group program, record group creation, final communication and teardown in each trainer. Include an application exception before the normal end and inspect how the intended policy coordinates or terminates remaining members. Acceptance should identify every process's cleanup outcome without depending on one final launcher message. This proposed check asserts no measured exit-time improvement and should not be interpreted as proof that a local finally block can repair every failed collective or unreachable rank.

## Keep reinitialization outside the claim

The checked reference labels destroy-then-reinitialize recovery during runtime unsupported or untested and calls for synchronization outside distributed primitives between those phases. Do not turn successful shutdown into a general promise of in-process fault recovery. The useful result is a coordinated end-of-job lifecycle for the selected groups and backend. Restarting after failure, restoring model state and handling missing participants require separately supported mechanisms; they cannot be established solely by calling teardown earlier or suppressing the resulting exit error.
