Skip to content

Fix parallel render() temp file deletion (#1632) - #2631

Merged
yihui merged 1 commit into
mainfrom
fix-1632-parallel-tmpfiles
Aug 25, 2026
Merged

yihui merged 1 commit into
mainfrom
fix-1632-parallel-tmpfiles

Conversation

@yihui

@yihui yihui commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

Problem

render() in parallel via a fork cluster (makeForkCluster, registerDoParallel(cores>1), foreach %dopar%, BiocParallel::MulticoreParam) intermittently fails with:

pandoc: /tmp/.../rmarkdown-str<hex>.html: openBinaryFile: does not exist (No such file or directory)
Error: pandoc document conversion failed with error 1

makePSOCKcluster never hit this — the tell.

Root cause

as_tmpfile() writes rmarkdown-str*.html files that Pandoc reads later during conversion (navbar, in_header/before_body/after_body includes). On render() exit, clean_tmpfiles() deleted every file matching that pattern in tempdir():

clean_tmpfiles <- function() {
  unlink(list.files(
    tempdir(), sprintf("^%s[0-9a-f]+[.]html$", tmpfile_pattern), full.names = TRUE
  ))
}

Forked workers share the parent's tempdir(), so one worker's cleanup deleted sibling workers' files while Pandoc was still using them. PSOCK workers get a separate tempdir() per process, hence no failure.

Diagnosed by @gorgitko in #1632 back in 2019; the long-standing workaround was stubbing out clean_tmpfiles via assignInNamespace().

Fix

Track files created by as_tmpfile() in .globals$tmpfiles and unlink only those. Each process cleans up exactly what it created; siblings are untouched. tempfile() already generates per-process unique names (verified: 3200 names across 16 forks, 0 collisions), so no cross-process name collision remains.

Test

Added a regression test in test-utils.R: a stray file matching the old glob pattern survives clean_tmpfiles(), while the process's own registered file is removed.

Scope

Fixes the parallel-render cause. The parallel-render reports in #1268 share this root cause; the network/UNC-drive cause in #1268 is separate and remains open.

as_tmpfile() writes rmarkdown-str*.html files that Pandoc reads later
during conversion (navbar, in-header/before-body/after-body includes).
On render() exit, clean_tmpfiles() removed every file matching that
pattern in tempdir(). Forked parallel workers share the parent's
tempdir(), so one worker's cleanup deleted sibling workers' files while
Pandoc was still using them, producing:

    pandoc: /tmp/.../rmarkdown-str<hex>.html: openBinaryFile: does not
    exist (No such file or directory)

This is why makePSOCKcluster (separate tempdir per process) worked while
makeForkCluster did not.

Track the files created by as_tmpfile() in .globals$tmpfiles and unlink
only those, so each process cleans up exactly what it created. tempfile()
already generates per-process unique names, so no cross-process name
collision remains.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@yihui
yihui merged commit 3ce1b28 into main Aug 25, 2026
17 checks passed
@yihui
yihui deleted the fix-1632-parallel-tmpfiles branch August 25, 2026 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pandoc: openBinaryFile: does not exist (No such file or directory) when using render() in parallel

1 participant