Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker was destroyed #389

Closed
David-moonsite opened this issue Dec 15, 2020 · 13 comments
Closed

Worker was destroyed #389

David-moonsite opened this issue Dec 15, 2020 · 13 comments
Labels
question Further information is requested
Milestone

Comments

@David-moonsite
Copy link

Hi ,
sometimes i get "Worker was destroyed",
why is it happening
Screen Shot 2020-12-15 at 10 04 39

@phuocng
Copy link
Member

phuocng commented Dec 15, 2020

sometimes

Can you let me know how to reproduce the issue?

@phuocng phuocng added the question Further information is requested label Dec 15, 2020
@David-moonsite
Copy link
Author

  • create list with 10 buttons
  • each button should open same modal with different Pdf Document
  • click on different button to open modal and close it several times

@phuocng
Copy link
Member

phuocng commented Dec 15, 2020

Where do you put the Worker?
It's recommended to put the Worker in the page, not in the modal.

@David-moonsite
Copy link
Author

In modal ,
i need to show pdf document in modal.

@phuocng
Copy link
Member

phuocng commented Dec 15, 2020

You should put the Worker in the page. In the Setting up the worker section, it mentions that we should place the Worker at the root:

const App = () => {
    return (
        <Worker
            workerUrl="https://unpkg.com/pdfjs-dist@2.5.207/build/pdf.worker.min.js"
        >
            ...
        </Worker>
    );
};

render(<App />, document.getElementById('root'));

@David-moonsite
Copy link
Author

OK,
i did it, same issue

@phuocng
Copy link
Member

phuocng commented Dec 15, 2020

@WiemSoued
Copy link

Same issue, even when the worker is placed at the root.
The message "Worker was destroyed" is shown then the pdf file is loaded.
Is there an explanation?

@WiemSoued
Copy link

The issue is that the pdf.worker.min.js is called multiple times so the message "Worker was destroyed" is present while the pdf.worker.min.js is pending.
Is there any explanation for this behavior?

@WiemSoued
Copy link

For anyone who needs this in the future,
the issue is caused when you use multiple workers on the same page.
If it's destroyed while still being initialized, you run into problems. The same happens if it's initialized while an earlier instance is still being destroyed.
I solved this by using a TimeOut.

@phuocng phuocng added this to the v2.3.2 milestone Jan 13, 2021
@phuocng
Copy link
Member

phuocng commented Jan 13, 2021

@David-moonsite
@WiemSoued
This issue is fixed in #424. I'll include the fix in v2.3.2!

Reason

By default, for each document pdf.js will create a web worker to parse and render the document.
The worker then will be reused if the it sees the same document.

Let's say that we have 3 documents, and there are 2 buttons, Next and Previous to load the previous and next document.
Initially, pdf.js will create a worker with id of d_0 for the first document.

When user clicks the Next button, it then destroys the d_0 worker, and create a new worker, d_1 to parse the second document.
If user clicks the Previous button, it will reuse the d_0 worker but the worker was destroyed.

In order to fix that, I create and destroy the worker manually.

@phuocng
Copy link
Member

phuocng commented Jan 19, 2021

@David-moonsite
@WiemSoued
Please upgrade to v2.3.2 which fixes the issue.

@phuocng phuocng closed this as completed Jan 19, 2021
@David-moonsite
Copy link
Author

Done, but now the top toolbar is gone ( download, zoom etc.... )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants