nats.net: Unity clients fail with IL2CPP builds

Update: The problem is with Unity. See https://github.com/nats-io/nats.net/issues/361#issuecomment-581632837 (below) for a workaround…

Update 2: The workaround doesn’t work, at least on iOS devices.

Update 3: Got it to work. See https://github.com/nats-io/nats.net/issues/361#issuecomment-593048632 (below)…

Update 4: It still fails sometimes even when the UseOldRequestStyle option is true.

Update 5: The problem has been solved. See: https://github.com/nats-io/nats.net/pull/370

I have a Unity project using NATS that works fine if built using Mono on Android or if using IL2CPP development build. If built as a release build with IL2CPP for Android or iOS, the NATS connection disconnects on the second request/reply and attempts to reconnect.

This appears to be similar to issue #358.

Here’s the NATS server log…

[8125] 2020/01/27 10:19:42.095095 [DBG] 66.60.161.134:40560 - cid:9 - Client connection created
[8125] 2020/01/27 10:19:42.158135 [TRC] 66.60.161.134:40560 - cid:9 - <<- [CONNECT {"verbose":false,"pedantic":false,"user":"","pass":"[REDACTED]","ssl_required":false,"name":"","auth_token":"","lang":".NET","version":"0.0.1","protocol":1,"jwt":"","nkey":"","sig":"","echo":false}]
[8125] 2020/01/27 10:19:42.158195 [TRC] 66.60.161.134:40560 - cid:9 - <<- [PING]
[8125] 2020/01/27 10:19:42.158216 [TRC] 66.60.161.134:40560 - cid:9 - ->> [PONG]
[8125] 2020/01/27 10:19:42.359824 [TRC] 66.60.161.134:40560 - cid:9 - <<- [SUB _INBOX.ec2db33d7faf4825ad7bba87047defaf.*  1]
[8125] 2020/01/27 10:19:42.359986 [TRC] 66.60.161.134:40560 - cid:9 - <<- [PUB Citizen.Login _INBOX.ec2db33d7faf4825ad7bba87047defaf.1 15]
[8125] 2020/01/27 10:19:42.360003 [TRC] 66.60.161.134:40560 - cid:9 - <<- MSG_PAYLOAD: ["\x92\xa4Frog\xa8blobblob"]
[8125] 2020/01/27 10:19:42.360016 [TRC] 127.0.0.1:45964 - cid:1 - ->> [MSG Citizen.Login 2 _INBOX.ec2db33d7faf4825ad7bba87047defaf.1 15]
[8125] 2020/01/27 10:19:42.361726 [TRC] 127.0.0.1:45964 - cid:1 - <<- [PONG]
[8125] 2020/01/27 10:19:42.361753 [TRC] 127.0.0.1:45964 - cid:1 - <<- [PUB _INBOX.ec2db33d7faf4825ad7bba87047defaf.1 11]
[8125] 2020/01/27 10:19:42.361767 [TRC] 127.0.0.1:45964 - cid:1 - <<- MSG_PAYLOAD: ["\x92\x00\xcf\x01\xf7\xf3w\xe6\x00\x00\x00"]
[8125] 2020/01/27 10:19:42.361786 [TRC] 66.60.161.134:40560 - cid:9 - ->> [MSG _INBOX.ec2db33d7faf4825ad7bba87047defaf.1 1 11]
[8125] 2020/01/27 10:19:42.804264 [TRC] 66.60.161.134:40560 - cid:9 - <<- [SUB Avatar.Pose.test  2]
[8125] 2020/01/27 10:19:42.804320 [TRC] 66.60.161.134:40560 - cid:9 - <<- [SUB Avatar.Select.test  3]
[8125] 2020/01/27 10:19:42.804352 [TRC] 66.60.161.134:40560 - cid:9 - <<- [SUB Prop.OnAdd.test  4]
[8125] 2020/01/27 10:19:42.804365 [TRC] 66.60.161.134:40560 - cid:9 - <<- [SUB Prop.OnMove.test  5]
[8125] 2020/01/27 10:19:42.804380 [TRC] 66.60.161.134:40560 - cid:9 - <<- [SUB Prop.OnRemove.test  6]
[8125] 2020/01/27 10:19:42.911325 [TRC] 66.60.161.134:40560 - cid:9 - <<- [PUB Prop.Look.test _INBOX.ec2db33d7faf4825ad7bba87047defaf.2 10]
[8125] 2020/01/27 10:19:42.911356 [TRC] 66.60.161.134:40560 - cid:9 - <<- MSG_PAYLOAD: ["\x91\xcf\x01\xf7\xf3w\xe6\x00\x00\x00"]
[8125] 2020/01/27 10:19:42.911371 [TRC] 127.0.0.1:46110 - cid:2 - ->> [MSG Prop.Look.test 2 _INBOX.ec2db33d7faf4825ad7bba87047defaf.2 10]
[8125] 2020/01/27 10:19:42.914473 [TRC] 127.0.0.1:46110 - cid:2 - <<- [PONG]
[8125] 2020/01/27 10:19:42.914499 [TRC] 127.0.0.1:46110 - cid:2 - <<- [PUB _INBOX.ec2db33d7faf4825ad7bba87047defaf.2 74]
[8125] 2020/01/27 10:19:42.914518 [TRC] 127.0.0.1:46110 - cid:2 - <<- MSG_PAYLOAD: ["\x92\x00\x81\xcf\x01\xeao_\x93\x01\x00\x00\x92\x02\x95\x93\xca\x00\x00\x00\x00\xca\x00\x00\x00\x00\xca\x00\x00\x00\x00\x94\xca\x00\x00\x00\x00\xca\x00\x00\x00\x00\xca\x00\x00\x00\x00\xca?\x80\x00\x00\xacTrackedImage\xa3Bar\xca>L\xcc\xcd"]
[8125] 2020/01/27 10:19:42.914534 [TRC] 66.60.161.134:40560 - cid:9 - ->> [MSG _INBOX.ec2db33d7faf4825ad7bba87047defaf.2 1 74]
[8125] 2020/01/27 10:19:42.967805 [DBG] 66.60.161.134:40560 - cid:9 - Client connection closed
[8125] 2020/01/27 10:19:42.967893 [TRC] 66.60.161.134:40560 - cid:9 - <-> [DELSUB 1]
[8125] 2020/01/27 10:19:42.967910 [TRC] 66.60.161.134:40560 - cid:9 - <-> [DELSUB 2]
[8125] 2020/01/27 10:19:42.967933 [TRC] 66.60.161.134:40560 - cid:9 - <-> [DELSUB 3]
[8125] 2020/01/27 10:19:42.967939 [TRC] 66.60.161.134:40560 - cid:9 - <-> [DELSUB 4]
[8125] 2020/01/27 10:19:42.967944 [TRC] 66.60.161.134:40560 - cid:9 - <-> [DELSUB 5]
[8125] 2020/01/27 10:19:42.967950 [TRC] 66.60.161.134:40560 - cid:9 - <-> [DELSUB 6]
[8125] 2020/01/27 10:19:43.241501 [DBG] 66.60.161.134:40562 - cid:10 - Client connection created
[8125] 2020/01/27 10:19:43.285678 [TRC] 66.60.161.134:40562 - cid:10 - <<- [CONNECT {"verbose":false,"pedantic":false,"user":"","pass":"[REDACTED]","ssl_required":false,"name":"","auth_token":"","lang":".NET","version":"0.0.1","protocol":1,"jwt":"","nkey":"","sig":"","echo":false}]
[8125] 2020/01/27 10:19:43.285773 [TRC] 66.60.161.134:40562 - cid:10 - <<- [PING]
[8125] 2020/01/27 10:19:43.285788 [TRC] 66.60.161.134:40562 - cid:10 - ->> [PONG]
[8125] 2020/01/27 10:19:43.352220 [TRC] 66.60.161.134:40562 - cid:10 - <<- [SUB _INBOX.ec2db33d7faf4825ad7bba87047defaf.*  1]
[8125] 2020/01/27 10:19:43.352270 [TRC] 66.60.161.134:40562 - cid:10 - <<- [SUB Avatar.Pose.test  2]
[8125] 2020/01/27 10:19:43.352300 [TRC] 66.60.161.134:40562 - cid:10 - <<- [SUB Avatar.Select.test  3]
[8125] 2020/01/27 10:19:43.352309 [TRC] 66.60.161.134:40562 - cid:10 - <<- [SUB Prop.OnAdd.test  4]
[8125] 2020/01/27 10:19:43.352374 [TRC] 66.60.161.134:40562 - cid:10 - <<- [SUB Prop.OnMove.test  5]
[8125] 2020/01/27 10:19:43.352406 [TRC] 66.60.161.134:40562 - cid:10 - <<- [SUB Prop.OnRemove.test  6]
[8125] 2020/01/27 10:19:43.444747 [TRC] 66.60.161.134:40562 - cid:10 - <<- [PING]
[8125] 2020/01/27 10:19:43.444806 [TRC] 66.60.161.134:40562 - cid:10 - ->> [PONG]
[8125] 2020/01/27 10:19:45.450564 [DBG] 66.60.161.134:40562 - cid:10 - Client Ping Timer
[8125] 2020/01/27 10:19:45.450651 [TRC] 66.60.161.134:40562 - cid:10 - ->> [PING]

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 29 (28 by maintainers)

Most upvoted comments

With the help of @ColinSullivan1 we were able to determine that Unity has a problem with their implementation of ConcurrentDictionary on IL2CPP builds. Since NATS uses ConcurrentDictionary, Unity IL2CPP builds with NATS won’t work.

I have opened a ticket with Unity at:

https://fogbugz.unity3d.com/default.asp?1216719_re63en8l0g4mhcsp

Even this simple example crashes Unity:

using System.Collections.Concurrent;
using UnityEngine;

public class Startup : MonoBehaviour {
	void Start() {
		var x = new ConcurrentDictionary<int, int>().Values;
	}
}

Workaround: If you build with the mono libraries, it works fine. If you need to build an IL2CPP version, changing the ConcurrentDictionary to a Dictionary based version of Conn.cs works:

Line 132 becomes:

private Dictionary<Int64, Subscription> subs = new Dictionary<Int64, Subscription>();

Line 3063 becomes:

if (!subs.ContainsKey(s.sid))
	return;
o = subs[s.sid];
subs.Remove(s.sid);

Line 3578 becomes:

public int SubscriptionCount {
	get {
		lock (mu) {
			return subs.Count;
		}
	}
}

Line 3646 adds:

lock (mu) {

Line 3673 adds:

}