@@ -464,6 +464,23 @@ void CompileCacheHandler::Persist() {
464464 continue ;
465465 }
466466 Debug (" -> %s\n " , mkstemp_req.path );
467+
468+ // Avoid leaking the descriptor or the temporary file if persistence
469+ // does not complete below.
470+ bool tmp_fd_needs_close = true ;
471+ bool tmp_renamed = false ;
472+ auto cleanup_tmp_file = OnScopeLeave ([&]() {
473+ if (tmp_fd_needs_close) {
474+ uv_fs_t req;
475+ uv_fs_close (nullptr , &req, mkstemp_req.result , nullptr );
476+ uv_fs_req_cleanup (&req);
477+ }
478+ if (!tmp_renamed) {
479+ uv_fs_t req;
480+ uv_fs_unlink (nullptr , &req, mkstemp_req.path , nullptr );
481+ uv_fs_req_cleanup (&req);
482+ }
483+ });
467484 Debug (" [compile cache] writing cache for %s %s to temporary file %s [%d "
468485 " %d %d "
469486 " %d %d]..." ,
@@ -496,6 +513,7 @@ void CompileCacheHandler::Persist() {
496513 auto cleanup_close =
497514 OnScopeLeave ([&close_req]() { uv_fs_req_cleanup (&close_req); });
498515 err = uv_fs_close (nullptr , &close_req, mkstemp_req.result , nullptr );
516+ tmp_fd_needs_close = false ;
499517
500518 if (err < 0 ) {
501519 Debug (" failed: %s\n " , uv_strerror (err));
@@ -521,6 +539,7 @@ void CompileCacheHandler::Persist() {
521539 Debug (" failed: %s\n " , uv_strerror (err));
522540 continue ;
523541 }
542+ tmp_renamed = true ;
524543 Debug (" success\n " );
525544 entry->persisted = true ;
526545 }
0 commit comments