archive: fix race condition in cmdStream #39860
- What I did
Fixed a race condition, see details in issue #39859
- How I did it
- How to verify it
There is a reproducer at https://github.com/stbenjam/docker-race-reproducer. Run the code as-is, and then apply the changes in this PR to vendor/github.com/docker/docker/pkg/archive/archive.go. You'll see the issue is fixed.
- Description for the changelog
Fixed a race condition in cmdStream to ensure that we wait for the command to exit before any clean up.
- A picture of a cute animal (not mandatory but encouraged)
My dog, Tilly:
The text was updated successfully, but these errors were encountered:
Please sign your commits following these rules:
$ git clone -b "cmd-race" email@example.com:stbenjam/moby.git somewhere $ cd somewhere $ git commit --amend -s --no-edit $ git push -f
Amending updates the existing PR. You DO NOT need to open a new one.
There is a race condition in pkg/archive when using `cmd.Start` for pigz and xz where the `*bufio.Reader` could be returned to the pool while the command is still writing to it, and then picked up and used by a new command. The command is wrapped in a `CommandContext` where the process will be killed when the context is cancelled, however this is not instantaneous, so there's a brief window while the command is still running but the `*bufio.Reader` was already returned to the pool. wrapReadCloser calls `cancel()`, and then `readBuf.Close()` which eventually returns the buffer to the pool. However, because cmdStream runs `cmd.Wait` in a go routine that we never wait for to finish, it is not safe to return the reader to the pool yet. We need to ensure we wait for `cmd.Wait` to finish! Signed-off-by: Stephen Benjamin <firstname.lastname@example.org>