Skip to content

Barrier.RemoveParticipant can throw undocumented BarrierPostPhaseException #125565

Closed

Description

@banbh

In System.Threading.Barrier method RemoveParticipants can throw a BarrierPostPhaseException however the documentation for that method only lists ObjectDisposedException and InvalidOperationException. The unit test below exhibits the issue by failing via an Assert.Inconclusive (which I chose since this may be a documentation issue rather than a code issue).

The source of the issue seems to be that both SignalAndWait and RemoveParticipant call FinishPhase and the latter may trigger a BPPE is the post-phase action throws.

        [TestMethod]
        public void Barrier_RemoveParticipant_ThrowsBPPE_WhenPostPhaseActionThrows()
        {
            const string msg = "post-phase failure";
            var barrier = new Barrier(2, _ => throw new InvalidOperationException(msg));
        
            var t = Task.Run(() =>
            {
                barrier.SignalAndWait(); // signal and wait for phase to complete
            });
            
            // Give task time to signal
            Thread.Sleep(50);
            
            // Now remove the second participant — this should trigger FinishPhase,
            // which runs the post-phase action, which throws, resulting in BPPE
            try
            {
                barrier.RemoveParticipant();
                Assert.Fail("Expected BarrierPostPhaseException was not thrown; " +
                            "behavior may have changed since test was written.");
            }
            catch (BarrierPostPhaseException bppe)
            {
                Assert.IsInstanceOfType<InvalidOperationException>(bppe.InnerException);
                Assert.AreEqual(msg, bppe.InnerException!.Message);
                Assert.Inconclusive("This is undocumented behavior!");
            }
            finally
            {
                // t.Wait(); // will throw
                barrier.Dispose();
            }
        }

Activity

  1. added
    untriaged
    on Mar 15, 2026
  2. dotnet-policy-service commented on Mar 15, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-runtime
    See info in area-owners.md if you want to be subscribed.

  3. removed
    untriaged
    on Mar 24, 2026
  4. locked and limited conversation to collaborators on Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions