diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-12-29 00:07:53 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-12-29 00:07:53 -0500 |
| commit | 0f9af5f44bd2735f34a48ceb177837a5a6ef25b0 (patch) | |
| tree | 427349e44d7e535fc00c389f96802a68895fd394 /src/stdlib | |
| parent | cfce376f585e0cd0231e95843617f75bd65b6c07 (diff) | |
Fix for file descriptors being reopened unintentionally
Diffstat (limited to 'src/stdlib')
| -rw-r--r-- | src/stdlib/paths.c | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/stdlib/paths.c b/src/stdlib/paths.c index cbe52f5f..dcedcd03 100644 --- a/src/stdlib/paths.c +++ b/src/stdlib/paths.c @@ -336,17 +336,19 @@ typedef struct { static Result_t _write_bytes_to_fd(List_t bytes, bool close_file, void *userdata) { writer_data_t *data = userdata; if (bytes.length > 0) { - int fd = open(data->path_str, data->mode, data->permissions); - if (fd == -1) { - if (errno == EMFILE || errno == ENFILE) { - // If we hit file handle limits, run GC collection to try to clean up any lingering file handles that - // will be closed by GC finalizers. - GC_gcollect(); - fd = open(data->path_str, data->mode, data->permissions); + if (data->fd == -1) { + data->fd = open(data->path_str, data->mode, data->permissions); + if (data->fd == -1) { + if (errno == EMFILE || errno == ENFILE) { + // If we hit file handle limits, run GC collection to try to clean up any lingering file handles + // that will be closed by GC finalizers. + GC_gcollect(); + data->fd = open(data->path_str, data->mode, data->permissions); + } + if (data->fd == -1) + return FailureResult("Could not write to file: ", data->path_str, " (", strerror(errno), ")"); } - if (fd == -1) return FailureResult("Could not write to file: ", data->path_str, " (", strerror(errno), ")"); } - data->fd = fd; if (bytes.stride != 1) List$compact(&bytes, 1); ssize_t written = write(data->fd, bytes.data, (size_t)bytes.length); |
