ioswebrtcstunturncoturn

Webrtc connection drops after 5 minutes on Apple devices (with coturn server)


I've setup a turn server for my WebRTC application. On Windows and Android, the application (and the turn server) works as expected. When I try the app on iOS or MacOS (currently only happening on Safari, and for some devices) the WebRTC connection works at the beginning, but after around 5 minutes it drops.

Here's the log from the turn server. The moment when the connection drops is around this line TLS/TCP socket buffer operation error (callback).

746: : session 000000000000000003: peer usage: realm=<metasim.io>, username=<testname>, rp=1767, rb=1642473, sp=281, sb=45819
750: : session 001000000000000006: peer 192.168.100.100 lifetime updated: 300
750: : session 001000000000000006: realm <metasim.io> user <testname>: incoming packet CREATE_PERMISSION processed, success
750: : session 001000000000000006: peer 37.63.64.9 lifetime updated: 300
750: : session 001000000000000006: realm <metasim.io> user <testname>: incoming packet CREATE_PERMISSION processed, success
750: : session 000000000000000003: peer 10.154.0.14 lifetime updated: 300
750: : session 000000000000000003: realm <metasim.io> user <testname>: incoming packet CREATE_PERMISSION processed, success
750: : session 000000000000000003: peer 10.154.0.14 lifetime updated: 600
750: : session 000000000000000003: realm <metasim.io> user <testname>: incoming packet CHANNEL_BIND processed, success
750: : session 000000000000000003: usage: realm=<metasim.io>, username=<testname>, rp=285, rb=47449, sp=1765, sb=1642092
750: : session 000000000000000003: peer usage: realm=<metasim.io>, username=<testname>, rp=1763, rb=1632229, sp=283, sb=46101
755: : session 000000000000000003: usage: realm=<metasim.io>, username=<testname>, rp=289, rb=47974, sp=1759, sb=1630876
755: : session 000000000000000003: peer usage: realm=<metasim.io>, username=<testname>, rp=1759, rb=1621207, sp=289, sb=46818
759: : session 000000000000000003: usage: realm=<metasim.io>, username=<testname>, rp=285, rb=47375, sp=1763, sb=1645836
759: : session 000000000000000003: peer usage: realm=<metasim.io>, username=<testname>, rp=1763, rb=1636011, sp=285, sb=46235
763: : session 000000000000000003: usage: realm=<metasim.io>, username=<testname>, rp=286, rb=47229, sp=1762, sb=1650592
763: : session 000000000000000003: peer usage: realm=<metasim.io>, username=<testname>, rp=1762, rb=1640926, sp=286, sb=46085
767: : session 000000000000000003: usage: realm=<metasim.io>, username=<testname>, rp=286, rb=47549, sp=1762, sb=1641564
767: : session 000000000000000003: peer usage: realm=<metasim.io>, username=<testname>, rp=1762, rb=1631749, sp=286, sb=46405
771: : session 000000000000000003: usage: realm=<metasim.io>, username=<testname>, rp=283, rb=47488, sp=1765, sb=1648212
771: : session 000000000000000003: peer usage: realm=<metasim.io>, username=<testname>, rp=1765, rb=1638599, sp=283, sb=46356
775: : session 000000000000000003: usage: realm=<metasim.io>, username=<testname>, rp=279, rb=47039, sp=1769, sb=1649852
775: : session 000000000000000003: peer usage: realm=<metasim.io>, username=<testname>, rp=1769, rb=1640051, sp=279, sb=45923
779: : session 000000000000000003: usage: realm=<metasim.io>, username=<testname>, rp=285, rb=47248, sp=1763, sb=1639700
779: : session 000000000000000003: peer usage: realm=<metasim.io>, username=<testname>, rp=1763, rb=1630019, sp=285, sb=46108
780: : session 001000000000000006: refreshed, realm=<metasim.io>, username=<testname>, lifetime=0, cipher=ECDHE-RSA-AES256-GCM-SHA384, method=TLSv1.2
780: : session 001000000000000006: realm <metasim.io> user <testname>: incoming packet REFRESH processed, success
780: : session 001000000000000006: TLS/TCP socket disconnected: 34.147.162.187:1090
780: : session 001000000000000006: usage: realm=<metasim.io>, username=<testname>, rp=8, rb=748, sp=8, sb=732
780: : session 001000000000000006: peer usage: realm=<metasim.io>, username=<testname>, rp=0, rb=0, sp=0, sb=0
780: : session 001000000000000006: closed (2nd stage), user <testname> realm <metasim.io> origin <>, local 0.0.0.0:3478, remote 34.147.162.187:1090, reason: TLS/TCP socket buffer operation error (callback)
780: : session 001000000000000006: delete: realm=<metasim.io>, username=<testname>
780: : session 001000000000000006: peer 37.63.64.9 deleted
780: : session 001000000000000006: peer 192.168.100.100 deleted

Here's also the turn.conf file:

# Realm settings
realm=<domain>

# Name
server-name=turnserver

fingerprint

# The TURN server IP address to be sent to external peers
external-ip=<ip>
listening-ip=0.0.0.0
listening-port=3478
min-port=10000
max-port=20000

# Validate credentials
lt-cred-mech
cli-password=<pass>

# Designate username and password
user=<user>:<pass>

# Certificate
cert=<path/to/certificate.crt>
pkey=<path/to/certificate.key>

# Logs
simple-log
verbose
log-file=/var/log/turn.log

# Possible fixes for 5-minutes bug, currently no success
#no-tlsv1
#no-tlsv1_1
#max-allocate-lifetime=36000
#permission-lifetime=36000
#stale-nonce=36000

This is a screenshot of Trickle ICE test of the turn server. Before I didn't had this error below (code=600), but either way the main problem was always there.

This is a list of things that we tried but were unsuccessful:

Expected result: to have unlimited WebRTC connection with our app for all possible devices using a turn server. Actual result: connection drops after around 5 minutes for Apple devices.

Any help is greatly appreciated!


Solution

  • It turns out the problem wasn't the turn server. It was the websocket closing after some time because it was not used after establishing connection (default timeout for the websocket is 300s as stated here).

    For our use case we fixed it by sending keep alive messages from the web client every 10 seconds (the interval seconds could be adjusted).