react-native-pdf icon indicating copy to clipboard operation
react-native-pdf copied to clipboard

App Crash with this.lastRNBFTask.cancel is not a function

Open riteshshah5432 opened this issue 1 year ago • 34 comments

react-native version: 0.72.4

react-native-pdf version: 6.7.4

What platform does your issue occur on? : Both

Describe your issue as precisely as possible :

  1. When the PDF is loading and try to back to the screen that time app crashes
  2. logs TypeError: this.lastRNBFTask.cancel is not a function (it is undefined)

Screenshot Simulator Screenshot - iPhone 15 Pro - 2023-12-23 at 18 09 22

Here is the code: <Pdf trustAllCerts={false} source={{ uri: URL }} onError={(error) => { console.log(error); }} onLoadComplete={(numberOfPages, filePath) => { console.log('completed') }} renderActivityIndicator={() => (<Loader />)} style={style.pdf} />

riteshshah5432 avatar Dec 23 '23 12:12 riteshshah5432

same issue

Subiyel avatar Dec 27 '23 10:12 Subiyel

do you find anything?

mdalishanali avatar Dec 31 '23 12:12 mdalishanali

+1

minh-dai avatar Jan 03 '24 07:01 minh-dai

+1

kartavyaparekh96 avatar Jan 04 '24 09:01 kartavyaparekh96

I have the same issue when browsing fast between different unloaded PDFs and download does not have time to finish before I start fetching new PDF.

This is due to this code introduced and merged about 2 weeks ago. In index.js, line 279, with declaration of this.lastRNBFTask.

BAD CODE: .catch(async (error) => { this._onError(error); });

comment it out and problem solved.

I am not an very experienced dev, but my guess is that the catch intercepts the cancel call that is supposed to be sent to ReactNativeBlobUtil and run its course there as it should, so instead it doesn't, and the code asks where the hell is my cancel method? Crash.

Well we caught the error before the cancel could do it's thing...

There is another similar catch on line 323. That one is fine, let it be.

alex-strae avatar Jan 05 '24 09:01 alex-strae

so, what should be better approach?

On Fri, Jan 5, 2024 at 3:04 PM alex-strae @.***> wrote:

I have the same issue when browsing fast between different unloaded PDFs and download does not have time to finish before I start fetching new PDF.

This is due to this code introduced and merged about 2 weeks ago. In index.js, line 279, with declaration of this.lastRNBFTask.

BAD CODE: .catch(async (error) => { this._onError(error); });

comment it out and problem solved.

I am not an very experienced dev, but my guess is that the catch intercepts the cancel call that is supposed to be sent to ReactNativeBlobUtil and run its course there as it should, so instead it doesn't, and the code asks where the hell is my cancel method? Crash.

Well we catched the error before the cancel could do it's thing...

There is another similar catch on line 323. That one is fine, let it be.

— Reply to this email directly, view it on GitHub https://github.com/wonday/react-native-pdf/issues/796#issuecomment-1878376691, or unsubscribe https://github.com/notifications/unsubscribe-auth/AV7OUDU2GGWOLRCCXOFOW3TYM7CJFAVCNFSM6AAAAABBAYXAFKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZYGM3TMNRZGE . You are receiving this because you commented.Message ID: @.***>

mdalishanali avatar Jan 05 '24 09:01 mdalishanali

Well, what do you want to do? For me, I had to have the app stop crashing when browsing fast between PDFs. The better approach for me was to comment out the recently introduced code (that I specified in last post).

alex-strae avatar Jan 05 '24 09:01 alex-strae

cool, thanks, I will also do like that.

On Fri, Jan 5, 2024 at 3:25 PM alex-strae @.***> wrote:

Well, what do you want to do? For me, I had to have the app stop crashing when browsing fast between PDFs. The better approach for me was to comment out the recently introduced code (that I specified in last post).

— Reply to this email directly, view it on GitHub https://github.com/wonday/react-native-pdf/issues/796#issuecomment-1878403772, or unsubscribe https://github.com/notifications/unsubscribe-auth/AV7OUDQRPAZFOZY6UHTLLCLYM7EZ3AVCNFSM6AAAAABBAYXAFKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZYGQYDGNZXGI . You are receiving this because you commented.Message ID: @.***>

mdalishanali avatar Jan 05 '24 10:01 mdalishanali

I have downgrade version @6.7.2 its working fine

kartavyaparekh96 avatar Jan 05 '24 10:01 kartavyaparekh96

Yes, because the code causing the issue was introduced just two weeks ago.HälsningarAlex5 jan. 2024 kl. 11:26 skrev kartavyaparekh96 @.***>: I have downgrade version @6.7.2 its working fine

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>

alex-strae avatar Jan 05 '24 10:01 alex-strae

I have downgrade version @6.7.2 its working fine

for me, it lead to a build issue. i used 6.7.3 and it seems fine. no crash

AminDannak avatar Jan 10 '24 13:01 AminDannak

I have the same issue when browsing fast between different unloaded PDFs and download does not have time to finish before I start fetching new PDF.

This is due to this code introduced and merged about 2 weeks ago. In index.js, line 279, with declaration of this.lastRNBFTask.

BAD CODE: .catch(async (error) => { this._onError(error); });

comment it out and problem solved.

I am not an very experienced dev, but my guess is that the catch intercepts the cancel call that is supposed to be sent to ReactNativeBlobUtil and run its course there as it should, so instead it doesn't, and the code asks where the hell is my cancel method? Crash.

Well we caught the error before the cancel could do it's thing...

There is another similar catch on line 323. That one is fine, let it be.

I also have the same issue, when I press back button it arise the same error, then I saw your comment and try to do the same thing as you mentioned. But still it arise error. It didn't crash the app but arise error on the onError props, but I don't want to arise any error on the cancel. Then I commented out the below code on the line 146, and it works for me.

// componentWillUnmount() {
//     this._mounted = false;
//     if (this.lastRNBFTask) {
//         this.lastRNBFTask.cancel(err => {
//         });
//         this.lastRNBFTask = null;
//     }
// }

shahid-0 avatar Jan 11 '24 21:01 shahid-0

Also happening to me any way to solve? Happening on latest also. @shahid-0 how does one deploy with commented out code? Is that possible?

abanobmikaeel avatar Jan 23 '24 13:01 abanobmikaeel

Make change(s) in node_modules, cd /ios , run pod install and you’re good.HälsningarAlex23 jan. 2024 kl. 14:34 skrev Abanob Mikaeel @.***>: Also happening to me any way to solve? Happening on latest also

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>

alex-strae avatar Jan 23 '24 16:01 alex-strae

+1 This is a blocker and I can't update this package anymore

UgurGumushan avatar Jan 31 '24 15:01 UgurGumushan

I have the same issue when browsing fast between different unloaded PDFs and download does not have time to finish before I start fetching new PDF.

This is due to this code introduced and merged about 2 weeks ago. In index.js, line 279, with declaration of this.lastRNBFTask.

BAD CODE: .catch(async (error) => { this._onError(error); });

comment it out and problem solved.

I am not an very experienced dev, but my guess is that the catch intercepts the cancel call that is supposed to be sent to ReactNativeBlobUtil and run its course there as it should, so instead it doesn't, and the code asks where the hell is my cancel method? Crash.

Well we caught the error before the cancel could do it's thing...

There is another similar catch on line 323. That one is fine, let it be.

Wow this is working really well. Spend alot of time on this issue and thank god u have solved my issue

IrfanVN avatar Feb 04 '24 17:02 IrfanVN

I was having the same issue. reinstalling the package with the latest one solved my crashing issue but then it was not crashing rather it was giving reactnativeblobutil error. by adding trustAllCerts={false} solved the not displaying pdf issue.

  <PDFView
          style={styles.pdfContainer}
          source={{uri: link}}
          onError={onError}
          trustAllCerts={false}
        />

Kashifmub205 avatar Feb 12 '24 16:02 Kashifmub205

I created this patch (patch-package) for avoid this crash:

react-native-pdf+6.7.4.patch

diff --git a/node_modules/react-native-pdf/index.js b/node_modules/react-native-pdf/index.js
index c7c58d8..70a6057 100644
--- a/node_modules/react-native-pdf/index.js
+++ b/node_modules/react-native-pdf/index.js
@@ -127,7 +127,7 @@ export default class Pdf extends Component {
 
         if ((nextSource.uri !== curSource.uri)) {
             // if has download task, then cancel it.
-            if (this.lastRNBFTask) {
+            if (this.lastRNBFTask?.cancel) {
                 this.lastRNBFTask.cancel(err => {
                     this._loadFromSource(this.props.source);
                 });
@@ -145,7 +145,7 @@ export default class Pdf extends Component {
 
     componentWillUnmount() {
         this._mounted = false;
-        if (this.lastRNBFTask) {
+        if (this.lastRNBFTask?.cancel) {
             this.lastRNBFTask.cancel(err => {
             });
             this.lastRNBFTask = null;
@@ -250,7 +250,7 @@ export default class Pdf extends Component {
 
     _downloadFile = async (source, cacheFile) => {
 
-        if (this.lastRNBFTask) {
+        if (this.lastRNBFTask?.cancel) {
             this.lastRNBFTask.cancel(err => {
             });
             this.lastRNBFTask = null;

KarlosQ avatar Feb 15 '24 10:02 KarlosQ

For me only @KarlosQ patch is working

neerajsingh-dev avatar Feb 23 '24 06:02 neerajsingh-dev

Even-though the patch prevents the app from crashing, it's not actually fixing the problem, because the cancel is not triggered so the download keeps going in background, which is not ideal.

Downgrading to 6.7.3 seems a better workaround imo, as it actually cancel the download as expected.

elkinjosetm avatar Mar 06 '24 21:03 elkinjosetm

@wonday Do you see a solution for this 6.7.4 regression?

KarlosQ avatar Mar 07 '24 09:03 KarlosQ

I created this patch (patch-package) for avoid this crash:

react-native-pdf+6.7.4.patch

diff --git a/node_modules/react-native-pdf/index.js b/node_modules/react-native-pdf/index.js
index c7c58d8..70a6057 100644
--- a/node_modules/react-native-pdf/index.js
+++ b/node_modules/react-native-pdf/index.js
@@ -127,7 +127,7 @@ export default class Pdf extends Component {
 
         if ((nextSource.uri !== curSource.uri)) {
             // if has download task, then cancel it.
-            if (this.lastRNBFTask) {
+            if (this.lastRNBFTask?.cancel) {
                 this.lastRNBFTask.cancel(err => {
                     this._loadFromSource(this.props.source);
                 });
@@ -145,7 +145,7 @@ export default class Pdf extends Component {
 
     componentWillUnmount() {
         this._mounted = false;
-        if (this.lastRNBFTask) {
+        if (this.lastRNBFTask?.cancel) {
             this.lastRNBFTask.cancel(err => {
             });
             this.lastRNBFTask = null;
@@ -250,7 +250,7 @@ export default class Pdf extends Component {
 
     _downloadFile = async (source, cacheFile) => {
 
-        if (this.lastRNBFTask) {
+        if (this.lastRNBFTask?.cancel) {
             this.lastRNBFTask.cancel(err => {
             });
             this.lastRNBFTask = null;

Would you like to create a pull request with those changes?

UgurGumushan avatar Mar 11 '24 12:03 UgurGumushan

Hey. Adding "cache: true" to source helped me. Like this: <Pdf source={{ uri: url, cache: true }}

kamiltenkai avatar Mar 12 '24 11:03 kamiltenkai

I have the same issue when browsing fast between different unloaded PDFs and download does not have time to finish before I start fetching new PDF. This is due to this code introduced and merged about 2 weeks ago. In index.js, line 279, with declaration of this.lastRNBFTask. BAD CODE: .catch(async (error) => { this._onError(error); }); comment it out and problem solved. I am not an very experienced dev, but my guess is that the catch intercepts the cancel call that is supposed to be sent to ReactNativeBlobUtil and run its course there as it should, so instead it doesn't, and the code asks where the hell is my cancel method? Crash. Well we caught the error before the cancel could do it's thing... There is another similar catch on line 323. That one is fine, let it be.

I also have the same issue, when I press back button it arise the same error, then I saw your comment and try to do the same thing as you mentioned. But still it arise error. It didn't crash the app but arise error on the onError props, but I don't want to arise any error on the cancel. Then I commented out the below code on the line 146, and it works for me.

// componentWillUnmount() {
//     this._mounted = false;
//     if (this.lastRNBFTask) {
//         this.lastRNBFTask.cancel(err => {
//         });
//         this.lastRNBFTask = null;
//     }
// }

thanks bro, that's work for me

nawazokz avatar Mar 15 '24 10:03 nawazokz

I created this patch (patch-package) for avoid this crash:

react-native-pdf+6.7.4.patch

diff --git a/node_modules/react-native-pdf/index.js b/node_modules/react-native-pdf/index.js
index c7c58d8..70a6057 100644
--- a/node_modules/react-native-pdf/index.js
+++ b/node_modules/react-native-pdf/index.js
@@ -127,7 +127,7 @@ export default class Pdf extends Component {
 
         if ((nextSource.uri !== curSource.uri)) {
             // if has download task, then cancel it.
-            if (this.lastRNBFTask) {
+            if (this.lastRNBFTask?.cancel) {
                 this.lastRNBFTask.cancel(err => {
                     this._loadFromSource(this.props.source);
                 });
@@ -145,7 +145,7 @@ export default class Pdf extends Component {
 
     componentWillUnmount() {
         this._mounted = false;
-        if (this.lastRNBFTask) {
+        if (this.lastRNBFTask?.cancel) {
             this.lastRNBFTask.cancel(err => {
             });
             this.lastRNBFTask = null;
@@ -250,7 +250,7 @@ export default class Pdf extends Component {
 
     _downloadFile = async (source, cacheFile) => {
 
-        if (this.lastRNBFTask) {
+        if (this.lastRNBFTask?.cancel) {
             this.lastRNBFTask.cancel(err => {
             });
             this.lastRNBFTask = null;

This worked for me. Many thanks!

dariyd avatar Mar 26 '24 11:03 dariyd

Would you like to create a pull request with those changes?

wonday avatar Mar 26 '24 16:03 wonday

Isn't that code kinda overwriting needed functionality? @wonday

As @elkinjosetm mentioned:

Even-though the patch prevents the app from crashing, it's not actually fixing the problem, because the cancel is not triggered so the download keeps going in background, which is not ideal.

abanobmikaeel avatar Apr 09 '24 02:04 abanobmikaeel

I was facing the same issue. Updating to the latest version 6.7.5 has fixed it.

chandwho avatar Apr 23 '24 14:04 chandwho