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 a few memory leaks #140

Closed
wants to merge 4 commits into from
Closed

Conversation

mrBliss
Copy link
Contributor

@mrBliss mrBliss commented Oct 7, 2022

This fixes the memory leaks identified in #137 and adds a workaround for another memory leak I don't fully understand. See the commit messages for more details.

There is no corresponding `free` for this `malloc` call in
`op_send_initial_metadata`, as `op`s are not recursively `free`d.

Fix it by referring directly to the metadata instead of copying it, as the
metadata will remain alive for the lifetime of the `Op`, see
`withOpArrayAndCtxts`.

Source: awakesecurity#137
There is no corresponding `grpc_byte_buffer_destroy` for this
`grpc_byte_buffer_copy` call in `op_send_message`, as `op`s are not recursively
`free`d.

Fix it by referring directly to the byte buffer instead of copying it, as the
byte buffer will remain alive for the lifetime of the `Op`, see
`withOpArrayAndCtxts`.

Source: awakesecurity#137
According to valgrind, a `grpc_slice` created by `grpc_slice_malloc_` (called
via `C.grpcSliceMalloc` in the `OpRecvStatusOnClient` case of `createOpContext`)
is leaked:

```
72 bytes in 1 blocks are definitely lost in loss record 8 of 11
   at 0x484079B: malloc (in /nix/store/73if83n79h4ml8rcb473ym47zmb4vi5x-valgrind-3.19.0/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
   by 0x4BD2ACD: gpr_malloc (in /nix/store/kfbhv9kid1dzblf16w9g1i10x48xkyrr-grpc-1.34.1/lib/libgpr.so.14.0.0)
   by 0x4AC3864: grpc_core::UnmanagedMemorySlice::HeapInit(unsigned long) (in /nix/store/kfbhv9kid1dzblf16w9g1i10x48xkyrr-grpc-1.34.1/lib/libgrpc.so.14.0.0)
   by 0x4AC3AC0: grpc_slice_malloc (in /nix/store/kfbhv9kid1dzblf16w9g1i10x48xkyrr-grpc-1.34.1/lib/libgrpc.so.14.0.0)
   by 0x41DCD1: grpc_slice_malloc_ (in ..)
   by 0x48D758: grpczmleakzm0zi0zi1zminplace_NetworkziGRPCziLowLevelziOp_createOpContext1_info (in ..)
```

However, we actually try to free this slice using `free_slice` in
`freeOpContext`, so this is unexpected.

Note that `grpc_slice` stores its payload on the heap and maintains its own
reference count, unless the payload is small enough to be stored inline. Perhaps
there is still an unknown reference to it somewhere, which keeps the
`grpc_slice` alive. I haven't found out why.

I did discover that decreasing `defaultStatusStringLen` and thus the size of the
slice made the memory leak disappear. I believe because a smaller size means
that no extra heap allocation is necessary and the payload can be stored inline.

As the comment on `defaultStatusStringLen` says that "gRPC actually ignores this
length and reallocates a longer string if necessary", decreasing its size should
be safe.
There is no corresponding `free` for this `malloc` call in
`op_send_status_server`, as `op`s are not recursively `free`d.

Fix it by referring directly to the metadata instead of copying it, as the
metadata will remain alive for the lifetime of the `Op`, see
`withOpArrayAndCtxts`.

Source: awakesecurity#137
@ixmatus
Copy link
Contributor

ixmatus commented Oct 13, 2022

@mrBliss thank you for submitting this PR! We've been really busy but should have some time soon to review your PR.

@fumieval
Copy link
Contributor

fumieval commented Jan 6, 2023

@ixmatus Did you have a chance to take a look at this?

@riz0id
Copy link
Collaborator

riz0id commented Jan 6, 2023

@fumieval I took a look, theres a few things I need to check but I plan on approving or leaving suggestions today.

@ixmatus
Copy link
Contributor

ixmatus commented Jan 6, 2023

@fumieval I took a cursory look but I'm just not familiar enough with this C-code to do a good job reviewing it, generally it seems fine though. A more experienced C-programmer looked at it as well and concluded they would need to swap in a lot more context about how the C-code works before being able to leave comments. I unfortunately don't have time to do that (and I don't think they do either).

Hopefully @riz0id does have the time but I do think we should be careful, especially with workarounds for a memory leak the original author didn't fully understand in 23b114e.

@fumieval
Copy link
Contributor

fumieval commented Mar 7, 2023

FWIW, we switched to this version in production and it seems to be working fine so far

@riz0id
Copy link
Collaborator

riz0id commented Jun 21, 2023

@fumieval is it fine if I merge master or could you? I still need to dig into this but master has some fixes to CI that I think are affecting this branch.

@fumieval fumieval mentioned this pull request Jun 22, 2023
@fumieval
Copy link
Contributor

@riz0id I've just submitted a PR with a branch rebased on top of master: #153

@riz0id
Copy link
Collaborator

riz0id commented Jun 22, 2023

@fumieval Thank you, I'm sorry I've been so slow with this. I've been really busy and theres still some CI issues on OSX we've been dealing with lately. I'm trying my best to find some time to review this. Thanks for being patient.

@fumieval
Copy link
Contributor

@riz0id No problem as we can always use a forked version.

There's a problem on OSX environment indeed cachix/install-nix-action#183

@riz0id
Copy link
Collaborator

riz0id commented Jun 23, 2023

@riz0id No problem as we can always use a forked version.

There's a problem on OSX environment indeed cachix/install-nix-action#183

Yeah, I've been dealing with that a lot lately. Waiting to see if #154 resolves the issue.

@riz0id
Copy link
Collaborator

riz0id commented Jun 23, 2023

Okay seems like upgrading to install-nix-action@v22 fixed the environment issue. I'll merge that fix to master so that you can merge it into #153. Your branch should pass CI after that.

@riz0id
Copy link
Collaborator

riz0id commented Jul 18, 2023

@fumieval I'm closing this and merging the rebased PR.

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.

4 participants