Producer is created but not found!

Some times (rarely) consuming failed due to invalid producer ID. See the log:

mediasoup:Producer constructor()
[conference:webrtc] producer id: 8915bff1-3d98-42a6-8b3a-d2d9887b7882
mediasoup:Transport consume()
mediasoup:Channel[pid:15973] request() [method:transport.consume, id:585]
mediasoup:ERROR:worker[pid:15973] RTC::Router::OnTransportNewConsumer() | throwing MediaSoupError: Producer not found [producerId:8915bff1-3d98-42a6-8b3a-d2d9887b7882]
mediasoup:WARN:Channel[pid:15973] request failed [method:transport.consume, id:585]: Producer not found [producerId:8915bff1-3d98-42a6-8b3a-d2d9887b7882]

Any idea why this is happening?

It’s your app the one calling transport.consume(), and not mediasoup itself. So if you use the producerId of a closed Producer, you get that error.

If it is closed then I should see related close log, right? As you can see in the log above, the producer is not closed and is being consumed right after the creation. This happens one in a thousand or even less.

The producer may be closed because you called close() on it or because you closed its transport. In the former you’ll see a log and in the latter as well. Check close() and transportClosed() methods:

So, if the producer is closed it’s closed due to something. Maybe you called close on the worker or on the router, which will close the transport, which will call producer.transportClosed(). There are no more options. So maybe you are calling transport.consume() by passing a producerId of an already closed Producer.