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

Report a coding issue that possibly leaks memory due to unclosed resources #8914

Closed
guodongli-google opened this issue Oct 4, 2021 · 0 comments · Fixed by #8917
Closed
Assignees
Labels
Projects

Comments

@guodongli-google
Copy link

Is this a bug report or feature request?

  • Bug Report

Deviation from expected behavior:

At https://github.com/rook/rook/blob/master/pkg/daemon/discover/discover.go#L156

stdout, err := cmd.StdoutPipe()

Pipe stdout is opened and then used in the scanner, however stdout is not closed after the usage, and cmd.Wait() is not called.

	cmd := exec.Command("stdbuf", "-oL", "udevadm", "monitor", "-u", "-k", "-s", "block")
	stdout, err := cmd.StdoutPipe()
	err = cmd.Start()
	scanner := bufio.NewScanner(stdout)

According to https://pkg.go.dev/os/exec#Cmd.StdoutPipe, cmd.Wait() will closed the pipe, but it is not invoked here.

stdout needs to be closed explicitly by invoking the closer, as demonstrated in:

defer stdOut.Close()

Expected behavior:
While this may not be a real issue depending on the underlying logic and the concrete resource and memory consumption, it is cleaner and safer to explicitly close the pipe after the usage:

	cmd := exec.Command("stdbuf", "-oL", "udevadm", "monitor", "-u", "-k", "-s", "block")
	stdout, err := cmd.StdoutPipe()
	defer stdOut.Close()

and/or terminate the cmd in the end by calling

	err = cmd.Wait()
	...

Found by static analyzer "DeepGo"

subhamkrai added a commit to subhamkrai/rook that referenced this issue Oct 5, 2021
Closing stdoutPipe that could possibly
leaks memory due to unclosed resources.

Closes: rook#8914
Signed-off-by: subhamkrai <srai@redhat.com>
subhamkrai added a commit to subhamkrai/rook that referenced this issue Oct 5, 2021
Closing stdoutPipe that could possibly
leaks memory due to unclosed resources.

Closes: rook#8914
Signed-off-by: subhamkrai <srai@redhat.com>
subhamkrai added a commit to subhamkrai/rook that referenced this issue Oct 5, 2021
Closing stdoutPipe that could possibly
leaks memory due to unclosed resources.

Closes: rook#8914
Signed-off-by: subhamkrai <srai@redhat.com>
subhamkrai added a commit to subhamkrai/rook that referenced this issue Oct 5, 2021
Closing stdoutPipe for the discovery daemon that
could possibly leaks memory due to unclosed resources.

Closes: rook#8914
Signed-off-by: subhamkrai <srai@redhat.com>
leseb pushed a commit to subhamkrai/rook that referenced this issue Oct 5, 2021
Closing stdoutPipe for the discovery daemon that
could possibly leaks memory due to unclosed resources.

Closes: rook#8914
Signed-off-by: subhamkrai <srai@redhat.com>
mergify bot pushed a commit that referenced this issue Oct 5, 2021
Closing stdoutPipe for the discovery daemon that
could possibly leaks memory due to unclosed resources.

Closes: #8914
Signed-off-by: subhamkrai <srai@redhat.com>
(cherry picked from commit 6387c7d)
@leseb leseb added this to To do in v1.7 via automation Oct 6, 2021
@leseb leseb moved this from To do to Done in v1.7 Oct 6, 2021
parth-gr pushed a commit to parth-gr/rook that referenced this issue Feb 22, 2022
Closing stdoutPipe for the discovery daemon that
could possibly leaks memory due to unclosed resources.

Closes: rook#8914
Signed-off-by: subhamkrai <srai@redhat.com>
parth-gr pushed a commit to parth-gr/rook that referenced this issue Feb 22, 2022
Closing stdoutPipe for the discovery daemon that
could possibly leaks memory due to unclosed resources.

Closes: rook#8914
Signed-off-by: subhamkrai <srai@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
v1.7
Done
Development

Successfully merging a pull request may close this issue.

2 participants