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

GC collects values it shouldn't with flambda #7861

vicuna opened this issue Oct 8, 2018 · 2 comments

GC collects values it shouldn't with flambda #7861

vicuna opened this issue Oct 8, 2018 · 2 comments


Copy link

@vicuna vicuna commented Oct 8, 2018

Original bug ID: 7861
Reporter: freyr
Assigned to: @lpw25
Status: resolved (set by @lpw25 on 2018-10-22T08:08:23Z)
Resolution: not a bug
Priority: high
Severity: minor
Platform: x86_64
OS: linux
OS Version: 4.18.11-arch1-1
Version: 4.07.1
Category: middle end (typedtree to clambda)
Monitored by: @nojb

Bug description

For some reason GC collects react events borrowed in Lwt_react in flambda build.E.keep or saving event in some variable or reference does not help either. Without flambda GC does not collect values it seems.

Steps to reproduce

A trivial example is attached. With flambda switch Lwt_react.E.with_finaliser functions print, without flambda they do not.

File attachments

Copy link

@vicuna vicuna commented Oct 8, 2018

Comment author: @stedolan

I had a look into this, and it seems to be an issue with the implementation of Lwt_react.with_finaliser rather than with the OCaml GC. That function is implemented as follows:

let with_finaliser f event =
let r = ref () in
Gc.finalise (finalise f) r;
map (fun x -> ignore r; x) event

The idea is to add a finaliser to the reference 'r', and ensure that the reference 'r' is reachable from the returned event. However, flambda (correctly!) notices that 'r' is not used in the returned event (since it is ignored), and optimises the use of 'r' away. This means that the GC is free to collect 'r' and run the finaliser immediately.

The simplest fix is to ensure that the reference to 'r' is not optimised away, by hiding the fact that it is unused from the optimiser using Sys.opaque_identity:

map (fun x -> ignore (Sys.opaque_identity r); x) event

I've made an issue on the Lwt bugtracker about this at ocsigen/lwt#626. I'm afraid I won't have time to follow up on this immediately (I'm just about to go on vacation for a couple of weeks), but I'll have a look later on.

Copy link

@vicuna vicuna commented Oct 14, 2018

Comment author: antron

This should be fixed in Lwt master in ocsigen/lwt#629.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

2 participants