Automatically close unused streams during garbage collection #5168
Labels
kind: enhancement
Label for issues suggesting enhancements; and for pull requests implementing enhancements
topic: library
It would be better if we automatically took care of closing streams whenever possible. I see two ways to achieve that:
Using finalizers requires work in the kernel: basically, whenever a stream holding a file descriptor is garbage collected, we could close its file descriptor (to avoid closing the same fd twice,
CloseStream
then should also make sure to do something likestream![1] := -1
). But finalizers can only be set in the kernel, and only for a whole TNUM at once... One could get around that in various ways, e.g. we could add aT_FILEDESC
TNUM just for this... I could go on, but let's rather look at the second option first.We could use a "weak pointer object": we already have
InputTextFileStillOpen
andOutputTextFileStillOpen
which hold all open file descriptors. I propose we merge them into one (not strictly necessary, but reduces the work needed and the general overhead), and add a companionWeakPointerObj
which tracks for eachfd
the corresponding stream.. So roughly like this (pseudo code):That does of course not make your patch redundant, but it would reduce the impact of people forgetting to use
CloseStream
Originally posted by @fingolfin in #5146 (comment)
The text was updated successfully, but these errors were encountered: