# consumer not found Error

**URL:** <https://mediasoup.discourse.group/t/consumer-not-found-error/1774>\
**Category:** mediasoup libraries\
**Created:** [September 20, 2020, 8:30am UTC](https://mediasoup.discourse.group/t/consumer-not-found-error/1774 "2020-09-20T08:30:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mustafa](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@mustafa](https://mediasoup.discourse.group/u/mustafa)\
**Post date:** [September 20, 2020, 8:30am UTC](https://mediasoup.discourse.group/t/consumer-not-found-error/1774/1 "2020-09-20T08:30:15Z")

</div>

Hi, sometimes I’m getting “consumer not found” error I don’t know what is causing this error it shows in the console log in my publishing server while thousands of users are working.

thanks,

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [September 20, 2020, 9:40am UTC](https://mediasoup.discourse.group/t/consumer-not-found-error/1774/2 "2020-09-20T09:40:04Z")

</div>

You are calling a method on a `Consumer` that was already closed. That’s all.

---

<div class="post-metadata">

**Author:** ![tomtseng](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/tomtseng/32/785_2.png) [@tomtseng](https://mediasoup.discourse.group/u/tomtseng)\
**Post date:** [January 28, 2021, 1:54am UTC](https://mediasoup.discourse.group/t/consumer-not-found-error/1774/3 "2021-01-28T01:54:16Z")

</div>

I’ve seen this error too with `consumer.setPreferredLayers()`, and when I catch the error and check `consumer.closed` and `consumer.paused`, they’re `false`. I also tried calling `consumer.getStats()` and `consumer.pause()` after catching, and those also throw an error, though `consumer.close()` still succeeds.

[EDIT: the following sentence is wrong]  
This happens when the associated producer closes while `consumer.setPreferredLayers()` is in progress. Weird that it doesn’t make `consumer.closed === true`.

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [January 28, 2021, 8:09am UTC](https://mediasoup.discourse.group/t/consumer-not-found-error/1774/4 "2021-01-28T08:09:50Z")

</div>

> [@tomtseng](#):
>
> I’ve seen this error too with `consumer.setPreferredLayers()` , and when I catch the error and check `consumer.closed` and `consumer.paused` , they’re `false` .

This is not true and I’ve confirmed it:

```js
videoProducer.close();

try {
  await videoConsumer.setPreferredLayers({ spatialLayer: 1 });
} catch (error) {
  console.warn('-------- catch(): %s', error);
  console.warn('-------- 2 videoConsumer.closed:', videoConsumer.closed);
}

```

`videoConsumer.closed === true` in the catch block as expected. This is super tested.

> [@tomtseng](#):
>
> though `consumer.close()` still succeeds.

No where in the docs it’s said that any mediasoup `close()` method throws if the entity is already closed. Nowhere.

> [@tomtseng](#):
>
> This happens when the associated producer closes while `consumer.setPreferredLayers()` is in progress. Weird that it doesn’t make `consumer.closed === true` .

Not true. Really.

---

<div class="post-metadata">

**Author:** ![tomtseng](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/tomtseng/32/785_2.png) [@tomtseng](https://mediasoup.discourse.group/u/tomtseng)\
**Post date:** [January 28, 2021, 11:53am UTC](https://mediasoup.discourse.group/t/consumer-not-found-error/1774/5 "2021-01-28T11:53:56Z")

</div>

> [@ibc](#):
>
> No where in the docs it’s said that any mediasoup `close()` method throws if the entity is already closed. Nowhere.

you’re right, `consumer.close()` succeeding isn’t useful information here.

I investigated my issue further and you’re right, the issue I saw isn’t caused by the producer closing while `setPreferredLayers()` is happening. It’s caused by the producer closing in the middle of creating the consumer with `transport.consume()`.

Here’s a toy way based off of [mediasoup-demo](https://github.com/versatica/mediasoup-demo) through which I can reproduce locally the issue that I was seeing. Replace [`Room.ts` lines 1576-1590](https://github.com/versatica/mediasoup-demo/blob/e80e6a154dd0ddf345f79cbc67b7084d302950a9/server/lib/Room.js#L1576-L1590) with

```javascript
                setTimeout(() => producer.close(), 0);
                consumer = await transport.consume(
                        {
                                producerId : producer.id,
                                rtpCapabilities : consumerPeer.data.rtpCapabilities,
                                paused : true
                        });
                logger.error("HERE CREATE", producer.id, consumer.closed, producer.closed);
                try {
                  await consumer.setPreferredLayers({ spatialLayer: 0 });
                  logger.error("HERE SUCCESS", producer.id, consumer.closed, producer.closed);
                } catch (e) {
                  logger.error("HERE CAUGHT", producer.id, consumer.closed, producer.closed);
                  logger.error(e);
                  await new Promise(r => setTimeout(r, 2000));
                  logger.error("HERE WAIT", producer.id, consumer.closed, producer.closed);
                }
                await new Promise(r => setTimeout(r, 2000));
                process.exit(1);

```

and then enter the room with two Chrome windows. It doesn’t always produce the error, but I occasionally get a trace like this:

```auto
2021-01-28T11:26:25.160Z mediasoup-demo-server:ERROR:Room HERE CREATE ae789c9b-a7ba-4670-940f-1d169c34f64b false true
...
2021-01-28T11:26:25.161Z mediasoup-demo-server:ERROR:Room HERE CAUGHT ae789c9b-a7ba-4670-940f-1d169c34f64b false true
2021-01-28T11:26:25.162Z mediasoup-demo-server:ERROR:Room Error: Consumer not found
    at Channel._processMessage (/Users/t/code/mediasoup-demo/server/node_modules/mediasoup/lib/Channel.js:199:37)
    at Socket.<anonymous> (/Users/t/code/mediasoup-demo/server/node_modules/mediasoup/lib/Channel.js:61:34)
    at Socket.emit (node:events:376:20)
    at Socket.EventEmitter.emit (node:domain:470:12)
    at addChunk (node:internal/streams/readable:305:12)
    at readableAddChunk (node:internal/streams/readable:280:9)
    at Socket.Readable.push (node:internal/streams/readable:219:10)
    at Pipe.onStreamRead (node:internal/stream_base_commons:192:23)
...
2021-01-28T11:26:27.162Z mediasoup-demo-server:ERROR:Room HERE WAIT ae789c9b-a7ba-4670-940f-1d169c34f64b false true

```

anyway, your original answer from September is still the answer to this issue—the associated producer is closed, so the consumer is dead and you can’t use it. It’s still surprising to me that `consumer.closed` sometimes prints as `false` even after two seconds of sleeping.

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [January 28, 2021, 12:20pm UTC](https://mediasoup.discourse.group/t/consumer-not-found-error/1774/6 "2021-01-28T12:20:06Z")

</div>

Please log `consumer.on('produceclose')` after the consumer is created to see when it happens.

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [January 28, 2021, 12:22pm UTC](https://mediasoup.discourse.group/t/consumer-not-found-error/1774/7 "2021-01-28T12:22:20Z")

</div>

> [@tomtseng](#):
>
> It’s still surprising to me that `consumer.closed` sometimes prints as `false` even after two seconds of sleeping.

It should be `true`.

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [January 28, 2021, 12:37pm UTC](https://mediasoup.discourse.group/t/consumer-not-found-error/1774/8 "2021-01-28T12:37:20Z")

</div>

Ok, I see the problem.

- When `setPreferredLayers()` succeeds, then immediately we get the ’ producerclose’ event in the Consumer so `consumer.closed === true` after that event.
- However, when `setPreferredLayers()` fails due to `Consumer not found`, the ‘producerclose’ event never happens so `consumer.closed === false` forever.

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [January 28, 2021, 12:47pm UTC](https://mediasoup.discourse.group/t/consumer-not-found-error/1774/9 "2021-01-28T12:47:57Z")

</div>

I see the problem. We are receiving the `producerclose` event for this Consumer before event we create the `Consumer` in the Node layer, so it does not receive the `producerclose` event.

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [January 28, 2021, 1:11pm UTC](https://mediasoup.discourse.group/t/consumer-not-found-error/1774/10 "2021-01-28T13:11:16Z")

</div>

Fixed and released in mediasoup 3.6.32:

> <https://github.com/versatica/mediasoup/issues/510>
>
> As explained here https://mediasoup.discourse.group/t/consumer-not-found-error/1774/

Thanks a lot @tomtseng!

---

<div class="post-metadata">

**Author:** ![tomtseng](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/tomtseng/32/785_2.png) [@tomtseng](https://mediasoup.discourse.group/u/tomtseng)\
**Post date:** [January 28, 2021, 5:33pm UTC](https://mediasoup.discourse.group/t/consumer-not-found-error/1774/11 "2021-01-28T17:33:30Z")

</div>

thanks for investigating! I was pleasantly surprised that you responded so quickly and now have fixed the anomaly so quickly

---

<div class="post-metadata">

**Author:** ![ibc](https://yyz2.discourse-cdn.com/free1/user_avatar/mediasoup.discourse.group/ibc/32/1540_2.png) [@ibc](https://mediasoup.discourse.group/u/ibc)\
**Post date:** [January 28, 2021, 6:43pm UTC](https://mediasoup.discourse.group/t/consumer-not-found-error/1774/12 "2021-01-28T18:43:34Z")

</div>

Your code snippet above helped 🙂
