Skip to content

WithTimeout's cancel func returns before c.err is written #100

@curt-hash

Description

@curt-hash

ctx, cancel := clockwork.WithTimeout(...)

cancel() closes cancelCalled and returns immediately without waiting for c.err to be written.

In my use case, this creates a data race with mock.AssertExpectations, which runs at the end of a test and reflects arguments to mocked methods (including this ctx).

I can work around the issue by doing something like:

defer func() {
  cancel()
  <-ctx.Done()
}()

But this is awkward. I do not expect a goroutine to mutate the context after cancel returns.

I think adding <-ctx.Done() after close(cancelCalled) would eliminate the race.

I also noticed that ctx.Err() blocks until Done is closed. This seems inconsistent with a normal context: // If Done is not yet closed, Err returns nil.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions