Skip to content

Future proofing: on.exit() stacking may cause issues. #41

@skiaphrene

Description

@skiaphrene

Hi there,

In the (QuasR 1.41.1) createAlignment functions, on.exit(expr, add=TRUE) is called multiple times, to record code that removes temporary files after the function finishes. "expr" typically contains R objects that contain filenames. However, there is a warning in the manual for on.exit():

The expr argument passed to on.exit is recorded without evaluation. If it is not subsequently removed/replaced by another on.exit call in the same function, it is evaluated in the evaluation frame of the function when it exits (including during standard error handling). Thus any functions or variables in the expression will be looked for in the function and its environment at the time of exit: to capture the current value in expr use substitute or similar.

This means that if the contents of the R objects changes between when it was logged first using on.exit() and when the function actually exits, then the code actually executed upon exit should not work on the original content. This behaviour can be seen with this code:
test <- function( ) { for(i in 1:4) { on.exit(print(i), add=TRUE) } } ; test() # => all return 4

Now, I did not actually see any places in the createAlignment functions where this would be an issue (e.g. in a loop), so the point is perhaps moot, but I wanted to check with you what you thought, and whether you think "future proofing" the code could be desirable - e.g. having a single on.exit() delete a vector of file names, and then simply adding to that vector as we progress through the function, rather than having one on.exit(...,add=TRUE) for each temporary file.

Thanks for your time! Best regards,

-- Alex

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions