Skip to content

Fix flaky testNoActiveInactiveNotificationsWhenDirectlyClosed - #3791

Open
iwang-1 wants to merge 1 commit into
apple:mainfrom
iwang-1:fix-flaky-directly-closed-test
Open

iwang-1 wants to merge 1 commit into
apple:mainfrom
iwang-1:fix-flaky-directly-closed-test

Conversation

@iwang-1

@iwang-1 iwang-1 commented Nov 13, 2026

Copy link
Copy Markdown

Fix a race that makes testNoActiveInactiveNotificationsWhenDirectlyClosed flaky.

Motivation:

ChannelNotificationTest.testNoActiveInactiveNotificationsWhenDirectlyClosed failed for me when running the full test suite with swift test --parallel on macOS (arm64):

ChannelNotificationTest.swift:552: error: -[NIOPosixTests.ChannelNotificationTest testNoActiveInactiveNotificationsWhenDirectlyClosed] : failed - There should be no events when the channel is closed directly.

It passed every time when run on its own. The test calls connect() from the test thread and only afterwards attaches the flatMap that closes the channel. If the event loop completes the connection and fires channelActive before the test thread attaches that callback, the handler sees channelActive and fails. That happens when the test thread is slow, for example when other tests run in parallel.

Adding a 100 ms sleep on the test thread between connect() and flatMap made the test fail in 3 of 3 runs.

Modifications:

  • Start the connection and attach the close callback inside eventLoop.flatSubmit, so the callback is attached before the event loop can complete the connection.
  • Replace the comment "The channel must still become active before it becomes inactive!", which contradicted what the test checks, with one that explains why the connect happens on the event loop.

Result:

The test no longer depends on thread timing. With the same 100 ms sleep added after flatSubmit, it passed in 3 of 3 runs. The change only touches this test.

🤖 Generated with Claude Code

The test called connect() from the test thread and attached the close
callback afterwards. If the event loop completed the connection first,
channelActive fired and the handler failed the test. This happens when
the test thread is slow, for example under parallel test runs.

Connect and attach the close callback inside eventLoop.flatSubmit so the
callback is always attached before the connection can complete.
@iwang-1
iwang-1 force-pushed the fix-flaky-directly-closed-test branch from b73a7ea to d6a6428 Compare November 18, 2026 23:01

This branch has not been deployed

No deployments
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.

1 participant