Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
GC collects values it shouldn't with flambda #7861
Original bug ID: 7861
For some reason GC collects react events borrowed in Lwt_react.E.select. 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
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 =
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:
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.