emscripten-core / emscripten-core/emscripten

Questions about Module.onExit

Open
#14,940 32 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

I see that there's an open issue regarding Module.onExit not being documented #6451. Is this simply an oversight or is there the possibility of Module.onExit going away?

I'm operating under the assumption that Module.onExit isn't going away, so my second question is about usage. I am building with -s ASYNCIFY and EXIT_RUNTIME=1 and using IDBFS for persistent storage. I have implemented the following:

```
Module["onExit"] = async function() {
await IO.closeFiles()
.catch(err => { console.log("Error closing files"); });
}
```

The IO.closeFiles() referenced above has the following implementation:

```
function closeFiles() {
_fsSynced = false;

return new Promise(
function (resolve, reject) {
FS.syncfs(false, function (err) {
if (err) {
reject(err);
}
else {
_fsSynced = true;
resolve(_fsSynced);
}
});
});
}
```
What I'm trying to accomplish is a final sync of the filesystem when my C++ code exits. My initial solution was to just initiate FS.syncfs from within the C++ code with EM_ASM and wait for the sync to complete using a loop and emscripten_sleep before exiting. That worked fine, but I'm using an open source code base, and I'm trying to avoid modifying files. I figured if there was a reliable way to sync from javascript after the C++ had exited, then I could avoid having to change the C++ source. The above code seems to be working, but I want to make sure it's reliable. I'm worried about a potential race condition and the possibility of data being destroyed before the FS.syncfs completes. Can I rely on this implementation or is there a better way to accomplish what I want?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.