Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data race in use of cmd output buffers. #58

Merged
merged 1 commit into from Dec 6, 2019

Conversation

sipsma
Copy link
Contributor

@sipsma sipsma commented Dec 5, 2019

The bytes.Buffers used to store the output of subprocesses are managed with a
sync.Pool. However, before this change, they were being returned to the Pool
while the slice underlying the Buffer was still in use by caller methods. If
that Buffer was then used by another caller, reads and writes to the underlying
slice had the potential race with one another (which is detectable by running
with the "-race" flag enabled in a binary and/or a test).

This change puts the responsibility on the caller of cmdOutput to put the buffer
back in the pool once it is done reading data from it, ensuring no two methods
use its slice at the same time.

Signed-off-by: Erik Sipsma sipsma@amazon.com

Fixes #57

The bytes.Buffers used to store the output of subprocesses are managed with a
sync.Pool. However, before this change, they were being returned to the Pool
while the slice underlying the Buffer was still in use by caller methods. If
that Buffer was then used by another caller, reads and writes to the underlying
slice had the potential race with one another (which is detectable by running
with the "-race" flag enabled in a binary and/or a test).

This change puts the responsibility on the caller of cmdOutput to put the buffer
back in the pool once it is done reading data from it, ensuring no two methods
use its slice at the same time.

Signed-off-by: Erik Sipsma <sipsma@amazon.com>
@codecov-io
Copy link

Codecov Report

Merging #58 into master will increase coverage by 7.39%.
The diff coverage is 17.39%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #58      +/-   ##
==========================================
+ Coverage    6.03%   13.43%   +7.39%     
==========================================
  Files           7        7              
  Lines         646      655       +9     
==========================================
+ Hits           39       88      +49     
+ Misses        600      544      -56     
- Partials        7       23      +16
Impacted Files Coverage Δ
utils.go 18.18% <0%> (+18.18%) ⬆️
runc.go 9.28% <19.04%> (+3.71%) ⬆️
monitor.go 65% <0%> (+65%) ⬆️
command_linux.go 70% <0%> (+70%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a2952bc...469fa2c. Read the comment docs.

Copy link
Member

@estesp estesp left a comment

Choose a reason for hiding this comment

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

LGTM

@crosbymichael
Copy link
Member

LGTM

@crosbymichael crosbymichael merged commit a5c2862 into containerd:master Dec 6, 2019
@crosbymichael
Copy link
Member

@sipsma thanks for the fix!

@sipsma sipsma deleted the gorace branch December 6, 2019 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Data Race with cmd output buffers
4 participants