com.unity.webrtc icon indicating copy to clipboard operation
com.unity.webrtc copied to clipboard

[BUG]: CreateAnswer sometimes creates an offer

Open streunerlein opened this issue 3 years ago • 7 comments

Package version

2.4.0-exp.9

Environment

* OS: Windows10
* Unity version: Unity 2021.2
* UnityWebrtc 2.4.0-exp.6 through 2.4.0-exp.9

Steps To Reproduce

  1. Create offer on one Computer (createOffer), set it as local description (setLocalDescription) and send it to other Computer
  2. On other Computer, setRemoteDescription and then createAnswer on the connection. Try to setLocalDescription on this answer.

Current Behavior

Sometimes (here it's 6 out of 10 times), the last call setLocalDescription fails with wrong state: have-remote-offer. Debugging it shows that the createAnswer in that situation created an offer instead of an answer.

Expected Behavior

createAnswer should always create an answer, no?

Anything else?

Can someone explain any circumstances that would make createAnswer return an offer? I don't think it ever should...

See screenshot appended.

Screenshot 2022-08-02 165329

streunerlein avatar Aug 02 '22 14:08 streunerlein

createAnswer should always create an answer, no?

Yeah, definitely. It might be bugs in our code.

karasusan avatar Aug 03 '22 02:08 karasusan

@streunerlein I checked our code, surely I found the code which might occurs the issue. We add the issue into our backlog.

karasusan avatar Aug 03 '22 02:08 karasusan

memo: WRS-387

karasusan avatar Aug 03 '22 02:08 karasusan

@karasusan Thank you very much.

October is a bit far away, I am thinking about a workaround. Do you have some more information on when this happens? Because I think the following naive solution is pretty much not going to work:

RTCSessionDescription desc = null;

while (desc == null || desc.type == RTCSdpType.Offer) {
  var op3 = pc.CreateAnswer();
  yield return op3;
  desc = op3.desc;
}

streunerlein avatar Aug 03 '22 07:08 streunerlein

@streunerlein I guess this issues might occur when running process of CreateOffer/CreateAnswer in parallel in native code.

karasusan avatar Aug 09 '22 03:08 karasusan

@karasusan Ok, thank you. Then indeed the naive solution might work. I got better results using this code:

            while (true) {
                var op3 = pc.CreateAnswer();
                yield return op3;
            
                if (!op3.IsError)
                {
                    if (op3.Desc.type != RTCSdpType.Answer)
                    {
                        Debug.Log($"Received {op3.Desc.type} instead of answer, retrying...");
                        continue;
                    }
                    yield return OnCreateAnswerSuccess(pc, op3.Desc);
                    break;
                }
                else
                {
                    break;
                }
            }

streunerlein avatar Aug 09 '22 10:08 streunerlein

Update: Of course, we've also done things wrong on our side creating these kind of race conditions. By correctly implementing perfect negotiation we haven't seen this phenomenon anymore.

streunerlein avatar Aug 12 '22 14:08 streunerlein