question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Race condition in document generation.

See original GitHub issue

I just started using the PDFKit. The following works after calling “doc.end()”:

// ... doc.end()
setTimeout(() => {
        res.download(`www/${fileName}`);
    }, 3000);

but this returns an empty file:

// ... doc.end()
res.download(`www/${fileName}`);

It appears that doc.end() is a synchronous function. Am I missing an async call, or is it a race condition on the disk?

If I try to read the file from disk immediately after calling doc.end() using fs, I get an empty file then also.

Thanks.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
alafrcommented, Dec 26, 2017

It is asynchronous:

var doc = new PDFDocument();
var stream = fs.createWriteStream('file.pdf');
stream.on('finish', function() {
  // The file is ready
});
doc.pipe(stream);
doc.end();

See also issues #642, #633, #525 and #265

0reactions
fabltdcommented, Mar 2, 2022

Yes it has PNG images

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is a Race Condition? - TechTarget
They occur when two computer program processes, or threads, attempt to access the same resource at the same time and cause problems in...
Read more >
Race condition - Wikipedia
In software. A race condition can arise in software when a computer program has multiple code paths that are executing at the same...
Read more >
Race conditions and deadlocks - Visual Basic - Microsoft Learn
A race condition occurs when two threads access a shared variable at the same time. The first thread reads the variable, and the...
Read more >
Race Conditions and Secure File Operations - Apple Developer
Avoiding Race Conditions. A race condition exists when changes to the order of two or more events can cause a change in behavior....
Read more >
Race Condition - HackTricks
Race conditions are vulnerabilities that appear in webs that limit the number of times you can perform an action. A very easy example...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found