Skip to content

Report coding issues that possibly leak memory due to unclosed resources #6084

@guodongli-google

Description

@guodongli-google

Description

The Writer is unclosed after usage, which may leak memory:

Steps to reproduce the issue

No response

Describe the results you received and expected

Consider the one:

w, err := cs.Writer(ctx2, content.WithRef(ref), content.WithDescriptor(ocispec.Descriptor{Size: size, Digest: d}))

w is of type content.Writer that contains a closer. Here w is not closed after the usage. It should be closed explicitly as demonstrated in another place in the same source file:
w2, err := cs.Writer(ctx, content.WithRef("c2"), content.WithDescriptor(ocispec.Descriptor{Size: int64(len(c2))}))
if err != nil {
t.Fatal(err)
}
defer w2.Close()

Note that w will not be closed within checkNewlyCreated() either:

func checkNewlyCreated(t *testing.T, w content.Writer, preStart, postStart, preUpdate, postUpdate time.Time) {
t.Helper()
st, err := w.Status()
if err != nil {
t.Fatalf("failed to get status: %v", err)
}
wd := w.Digest()

Fix

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 write after the usage, as so in other cases.

	w, err := cs.Writer(ctx2, content.WithRef(ref), content.WithDescriptor(ocispec.Descriptor{Size: size, Digest: d}))
	defer w.Close()
	if err != nil {
		t.Fatal(err)
	}

Found by static analyzer "DeepGo"

What version of containerd are you using?

Current github version

Any other relevant information

No response

Show configuration if it is related to CRI plugin.

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions