Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

Fix race condition in Mock.AfterFunc() #52

Merged
merged 2 commits into from
Apr 19, 2023

Conversation

nhawke
Copy link

@nhawke nhawke commented Apr 19, 2023

Previously, AfterFunc() created a timer using Timer() and then added a
function callback to the timer. This created a data race because the
timer could fire between when it is created by Timer() and when the
function callback is set.

Instead, this change created the Timer{} struct and adds it to the
Mock's list of timers inline. This ensures that the function callback is
set before the Timer is able to fire.

The included test was originally authored by @abhinav

nhawke added 2 commits April 19, 2023 11:51
Previously, AfterFunc() created a timer using Timer() and then added a
function callback to the timer. This created a data race because the
timer could fire between when it is created by Timer() and when the
function callback is set.

Instead, this change created the Timer{} struct and adds it to the
Mock's list of timers inline. This ensures that the function callback is
set before the Timer is able to fire.
Copy link
Collaborator

@djmitche djmitche left a comment

Choose a reason for hiding this comment

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

Thanks! I think this is a better fix than #42.

@djmitche djmitche merged commit f9fef96 into benbjohnson:master Apr 19, 2023
@djmitche
Copy link
Collaborator

(released in v1.3.1)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants