Skip to content

Race condition causes builds to cancel themselves when triggered during active build #784

@assembled-dylan

Description

@assembled-dylan

Description

When a file change triggers a build while an existing build is running, a race condition in engine.go causes the new build to cancel itself, making the running binary outdated.

Root cause

You make a change to trigger build A. In the buildRun() function, build A will put true onto buildRunCh, and then proceed with its build. While it is building, you make a change to trigger build B. In the start() function, build B sees that there is a value on buildRunCh, so it puts a value onto buildRunStopCh:

// already build and run now
select {
case <-e.buildRunCh:
  e.buildRunStopCh <- true
default:
}

In then calls go buildRun(). Since buildRunCh is now empty, it will be able to place true onto it. Directly after, it will see that buildRunStopCh is not-empty, and will effectively cancel itself.

select {
case <-e.buildRunStopCh:
  return
default:
}

Reproduction

Place the following in your .air.toml file to make the build process take a while. Make a change, wait a little bit (less than 10 seconds), and then make another change - the second one will not be reflected in the running code.

cmd = "echo 'Build started at' $(date +%H:%M:%S) && go build -o ./tmp/main . && echo 'Build complete' && sleep 10"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions