Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007861OCamlmiddle end (typedtree to clambda)public2018-10-08 10:562018-10-22 10:08
Assigned Tolpw25 
StatusresolvedResolutionno change required 
Platformx86_64OSlinuxOS Version4.18.11-arch1-1
Product Version4.07.1 
Target VersionFixed in Version 
Summary0007861: GC collects values it shouldn't with flambda
DescriptionFor 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 ReproduceA trivial example is attached. With flambda switch `Lwt_react.E.with_finaliser` functions print, without flambda they do not.
TagsNo tags attached.
Attached Filesgz file icon gc.tar.gz [^] (825 bytes) 2018-10-08 10:56

- Relationships

-  Notes
stedolan (developer)
2018-10-08 15:40

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 [^] 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.
antron (reporter)
2018-10-14 03:38

This should be fixed in Lwt master in [^]

- Issue History
Date Modified Username Field Change
2018-10-08 10:56 freyr New Issue
2018-10-08 10:56 freyr File Added: gc.tar.gz
2018-10-08 15:40 stedolan Note Added: 0019404
2018-10-14 03:38 antron Note Added: 0019413
2018-10-22 10:08 lpw25 Status new => resolved
2018-10-22 10:08 lpw25 Resolution open => no change required
2018-10-22 10:08 lpw25 Assigned To => lpw25

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker