# External youtube-dl-exec library crashes NR

**URL:** https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807
**Category:** General
**Tags:** function-node
**Created:** [13 March 2022 17:19 UTC](https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807 "2022-03-13T17:19:58Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [13 March 2022 17:19 UTC](https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807/1 "2022-03-13T17:19:58Z")

</div>

In January 2022 - I had the following flow, using a function Node to get information about a YouTube video. It uses the youtube-dl-exec library.

The function code was quite simple:

```auto
const myTimeout = setTimeout(NoReturn, 5000);

youtubeDlExec(msg.payload, {dumpSingleJson: true}).then(output => {
    msg.payload = output;
    clearTimeout(myTimeout);
    node.send(msg);
    node.done();
    });

function NoReturn(){
 msg.payload = "No response";
 node.send(msg);
 node.done();
}

```

In case a wrong URL was specified - I became - as expected a "No response" message.

**Now NR crashes complete.**... and I don't know why.

I tried to change the function code - to use try... , catch ... . But nothing helps.  
I have update NR and this library to the newest version.

If a correct URL is provided - the function works well. So what can I do make this function resistent against wrong inputs without crashing NR.??

Here is the complete flow:

```auto
[
    {
        "id": "6452b757c45baa7c",
        "type": "function",
        "z": "7fd8fff652f013f7",
        "name": "youtube-dl-exec",
        "func": "const myTimeout = setTimeout(NoReturn, 5000);\n\nyoutubeDlExec(msg.payload, {dumpSingleJson: true}).then(output => {\n msg.payload = output;\n clearTimeout(myTimeout);\n node.send(msg);\n node.done();\n });\n\n\nfunction NoReturn(){\n msg.payload = \"No response\";\n node.send(msg);\n node.done();\n}\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [
            {
                "var": "youtubeDlExec",
                "module": "youtube-dl-exec"
            }
        ],
        "x": 500,
        "y": 1700,
        "wires": [
            [
                "68d5040203c27b4d"
            ]
        ]
    },
    {
        "id": "6c856e483f45a597",
        "type": "inject",
        "z": "7fd8fff652f013f7",
        "name": "6xKWiCMKKJg",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "https://www.youtube.com/watch?v=6xKWiCMKKJg",
        "payloadType": "str",
        "x": 300,
        "y": 1700,
        "wires": [
            [
                "6452b757c45baa7c"
            ]
        ]
    },
    {
        "id": "68d5040203c27b4d",
        "type": "debug",
        "z": "7fd8fff652f013f7",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 710,
        "y": 1700,
        "wires": []
    },
    {
        "id": "9904e080ec7c54de",
        "type": "inject",
        "z": "7fd8fff652f013f7",
        "name": "Err6xKWiCMKKJg",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "https://www.youtube.com/watch?v=Err6xKWiCMKKJg",
        "payloadType": "str",
        "x": 310,
        "y": 1660,
        "wires": [
            [
                "6452b757c45baa7c"
            ]
        ]
    }
]

```

So - OK I updated all NR - but what has so fundamentally changed - that this function node is able to crash NR?

The last version when it didn't crash was 2.1.4 or earlier.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [13 March 2022 19:05 UTC](https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807/2 "2022-03-13T19:05:48Z")

</div>

You are missing any error handling in the call to `youtubeDlExec`. It returns a Promise object, so you need to define a `catch` function:

```auto
youtubeDlExec(msg.payload, {dumpSingleJson: true}).then(output => {
    msg.payload = output;
    clearTimeout(myTimeout);
    node.send(msg);
    node.done();
}).catch(err => {
    // An error has been returned by the Promise.
    // Do something with it...
});

```

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [13 March 2022 19:50 UTC](https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807/3 "2022-03-13T19:50:11Z")

</div>

> [@knolleary](#):
>
> ```auto
> .catch(err => {
> // An error has been returned by the Promise.
> // Do something with it...
> });
> 
> ```

Great this works. Many thanks!!!!

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [13 March 2022 23:48 UTC](https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807/4 "2022-03-13T23:48:14Z")

</div>

Only one question - I am happy that this works in that way, but in the past I have not to catch this. And in general is this not a problem, that the whole system crashed? I have no problem as I know now how to handle this, but I think the system should at least be able to handle this somehow?

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [13 March 2022 23:51 UTC](https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807/5 "2022-03-13T23:51:40Z")

</div>

There is no safe way to handle uncaught, asynchronous errors. We have no way of knowing what state the entire system has been left in and whether it is safe to let it continue running.

To quote the [Node.js docs](https://nodejs.org/api/process.html#warning-using-uncaughtexception-correctly):

> The correct use of 'uncaughtException' is to perform synchronous cleanup of allocated resources (e.g. file descriptors, handles, etc) before shutting down the process. **It is not safe to resume normal operation after 'uncaughtException'**.

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [13 March 2022 23:55 UTC](https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807/6 "2022-03-13T23:55:15Z")

</div>

OK - but why was this not a problem 2 months ago? What has been changed? OK it can be the library has changed as well. In the past there was simple no answer - this is why it was possible to catch the error with a simple timeout.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [13 March 2022 23:55 UTC](https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807/7 "2022-03-13T23:55:43Z")

</div>

> [@mickym2](#):
>
> I am happy that this works in that way, but in the past I have not to catch this

I have a memory of that in older versions of nodejs, a promise without a catch would not crash an application. About nodejs v10 they started putting warning in the console that this would change & would exit nodejs - I think this was introduced in v14 or v16 (I forget)

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [13 March 2022 23:59 UTC](https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807/8 "2022-03-13T23:59:58Z")

</div>

2 months ago I had at least nodejs v14 - but it can be that I updated it to v16. I can try on another systems with v14 - but this would explain the change in behavior. 😉 - Thanks.  
.

Edit: Ok crashed also on nodejs v14. I do not know - why it was not necessary in the past.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [14 March 2022 00:21 UTC](https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807/9 "2022-03-14T00:21:02Z")

</div>

I found a copy of the message. It's funny how time flies.

> Deprecation warning in Node 7 states that this will be changed in future Node.js versions:
> 
> Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

So it might have changed in V8 or v10

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [14 March 2022 00:30 UTC](https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807/10 "2022-03-14T00:30:17Z")

</div>

It was definitely not such a long time ago. As I said - in January 2022 it works well with the code in my first post in this thread. I only thought there was an internal mechanism in NR which handled this exception - if not - I do not know and may be it is not worthy to think about it, but I thought if we definitely know why it was working, we can do something to improve stability.

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [14 March 2022 02:56 UTC](https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807/11 "2022-03-14T02:56:19Z")

</div>

Just as an aside - the youtube-dl code is pretty flaky and does not work well all the time - it slows down dramatically when downloading videos.

The better option is

yt-dlp

Craig

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [28 March 2022 02:57 UTC](https://discourse.nodered.org/t/external-youtube-dl-exec-library-crashes-nr/59807/12 "2022-03-28T02:57:18Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
