Skip to content

Fix logger initialization continuation race in LoggingBus#8006

Merged
Aaronontheweb merged 10 commits into
akkadotnet:devfrom
Aaronontheweb:fix/logger-init-continuation-race
Jan 25, 2026
Merged

Fix logger initialization continuation race in LoggingBus#8006
Aaronontheweb merged 10 commits into
akkadotnet:devfrom
Aaronontheweb:fix/logger-init-continuation-race

Conversation

@Aaronontheweb
Copy link
Copy Markdown
Member

Summary

Fixes flaky EventFilterDebugTests where "Logger started" message was published during test teardown.

Problem

StartDefaultLoggers was only waiting for the Ask task (logger ACK), not the continuation that publishes "Logger started" message. This caused the continuation to run during test teardown, triggering EventFilter assertions.

Solution

The fix makes two changes to AddLogger:

  1. Returns the continuation task instead of the Ask task, so Task.WaitAll waits for the full initialization sequence
  2. Uses TaskContinuationOptions.ExecuteSynchronously to ensure the continuation runs immediately when the Ask completes

Changes

  • src/core/Akka/Event/LoggingBus.cs: Return continuation task from AddLogger() and add ExecuteSynchronously option

Test plan

  • Verified EventFilterDebugTests.Messages_can_be_muted_from_now_on_with_using passes 10/10 runs
  • Both EventFilterDebugTests and CustomEventFilterDebugTests variants pass

StartDefaultLoggers was only waiting for the Ask task (logger ACK), not the
continuation that publishes "Logger started" message. This caused the
continuation to run during test teardown, triggering EventFilter assertions.

The fix returns the continuation task from AddLogger and uses
TaskContinuationOptions.ExecuteSynchronously to ensure the continuation
runs immediately when the Ask completes, before WaitAll returns.
Copy link
Copy Markdown
Member Author

@Aaronontheweb Aaronontheweb left a comment

Choose a reason for hiding this comment

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

I should point out that even though this fix touches core Akka, it's mostly just a problem for our test suite.

});
return (askTask, fullLoggerName);
}, TaskContinuationOptions.ExecuteSynchronously);
return (continuationTask, fullLoggerName);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Return the continuation task and have waiters wait on that, versus the original task itself.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just noticed this now, but if askTask threw, the continuation would fail on var response = t.Result; and the logger would not be removed/unsubscribed. Maybe we need to check for t.IsFaulted too?

@Aaronontheweb Aaronontheweb enabled auto-merge (squash) January 22, 2026 17:49
@Aaronontheweb Aaronontheweb enabled auto-merge (squash) January 22, 2026 19:38
Copy link
Copy Markdown
Contributor

@Arkatufus Arkatufus left a comment

Choose a reason for hiding this comment

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

Comment not relayed to the actual fix

});
return (askTask, fullLoggerName);
}, TaskContinuationOptions.ExecuteSynchronously);
return (continuationTask, fullLoggerName);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just noticed this now, but if askTask threw, the continuation would fail on var response = t.Result; and the logger would not be removed/unsubscribed. Maybe we need to check for t.IsFaulted too?

Handle the case where the Ask operation fails with an exception,
not just when it's cancelled. This ensures the logger is properly
removed and unsubscribed if initialization throws an exception.
}


// Ask operation failed with an exception
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Do this address your concern @Arkatufus ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Contributor

@Arkatufus Arkatufus left a comment

Choose a reason for hiding this comment

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

LGTM

@Aaronontheweb Aaronontheweb added this to the 1.5.59 milestone Jan 22, 2026
@Aaronontheweb Aaronontheweb merged commit 4b4c6f4 into akkadotnet:dev Jan 25, 2026
12 checks passed
Aaronontheweb added a commit to Aaronontheweb/akka.net that referenced this pull request Jan 26, 2026
…#8006)

* Fix logger initialization continuation race in LoggingBus

StartDefaultLoggers was only waiting for the Ask task (logger ACK), not the
continuation that publishes "Logger started" message. This caused the
continuation to run during test teardown, triggering EventFilter assertions.

The fix returns the continuation task from AddLogger and uses
TaskContinuationOptions.ExecuteSynchronously to ensure the continuation
runs immediately when the Ask completes, before WaitAll returns.

* Add IsFaulted check for logger initialization

Handle the case where the Ask operation fails with an exception,
not just when it's cancelled. This ensures the logger is properly
removed and unsubscribed if initialization throws an exception.
Aaronontheweb added a commit that referenced this pull request Jan 26, 2026
* Fix logger initialization continuation race in LoggingBus

StartDefaultLoggers was only waiting for the Ask task (logger ACK), not the
continuation that publishes "Logger started" message. This caused the
continuation to run during test teardown, triggering EventFilter assertions.

The fix returns the continuation task from AddLogger and uses
TaskContinuationOptions.ExecuteSynchronously to ensure the continuation
runs immediately when the Ask completes, before WaitAll returns.

* Add IsFaulted check for logger initialization

Handle the case where the Ask operation fails with an exception,
not just when it's cancelled. This ensures the logger is properly
removed and unsubscribed if initialization throws an exception.
This was referenced May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants