Skip to content

fix(core): Memory leak bugs in CheckPoint::drop impl #1997

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

evanlinjin
Copy link
Member

@evanlinjin evanlinjin commented Jul 20, 2025

Description

  • Fix memory leak bug in CheckPoint::drop by using Arc::into_inner if it is available (>= 1.70).
  • Fix CPInner::drop logic so that if CPInner::block becomes generic and is of a type that required drop, it does not leak memory.
  • Add tests for memory leak + stack overflow when dropping CheckPoint.

An alternative fix is to bump MSRV to >= 1.70 so we don't need to do conditional compilation based on version.

Changelog notice

Fix:
- Drop implementation of `CheckPoint` to avoid memory leaks for rust version >= 1.70.

Checklists

All Submissions:

Bugfixes:

  • I've added tests to reproduce the issue which are now passing

Fix memory leak bug in `CheckPoint::drop` by using `Arc::into_inner` if
it is available (>= 1.70).

Fix `CPInner::drop` logic so that if `CPInner::block` becomes
generic and is of a type that required `drop`, it does not leak memory.

Add tests for memory leak + stack overflow when dropping `CheckPoint`.
@evanlinjin evanlinjin force-pushed the fix/checkpoint-drop-memory-leak branch 2 times, most recently from f7632fb to 4401886 Compare July 20, 2025 06:59
Comment on lines -43 to -44
// Don't call `drop` on `CPInner` since that risks it becoming recursive.
core::mem::forget(node);
Copy link
Member Author

Choose a reason for hiding this comment

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

Rationale for removal:

core::mem::forget is dangerous if CPInner::block is generic since the type might require a call to drop. This is not possible now, however it's best to remove it now to avoid forgetting about it later on.

The initial motivation for calling forget is to ensure no recursion happens. However, the recursive depth would always only be 1 since prev is None. Thus, there is no risk of stack overflow (proven by the checkpoint_drop_is_not_recursive test that is added).

Copy link
Contributor

@LagginTimes LagginTimes left a comment

Choose a reason for hiding this comment

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

ACK 4401886

if version_check::is_min_version("1.70.0").unwrap_or(false) {
println!("cargo:rustc-cfg=has_arc_into_inner");
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO this is slightly overkill and we can wait until MSRV is at least 1.70.0 to use Arc::into_inner.

Copy link
Member Author

Choose a reason for hiding this comment

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

Memory leaks aren't fun though.

@tnull
Copy link
Contributor

tnull commented Jul 23, 2025

An alternative fix is to bump MSRV to >= 1.70 so we don't need to do conditional compilation based on version.

FWIW, bumping MSRV to 1.75 would be fine by us, especially if it allows you guys to avoid these kinds of complications (also cf. #1750)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

4 participants