Race condition when shutting down TURN transport
The TurnTransport::Impl::shutdown
function currently calls pj_sock_destroy
while the ioWorker
thread is still running:
if (relay) {
pj_turn_sock_destroy(relay);
relay = nullptr;
}
turnLock.reset();
if (ioWorker.joinable())
ioWorker.join();
This can cause an assert to fail in pj_turn_session_alloc
(cf. jami-daemon#974 (closed)) because pj_turn_sock_destroy
sets the state of the TURN session to PJ_TURN_STATE_DESTROYING
, and some of the callbacks called in the ioWorker
thread expect the session to be in a different state:
jami: ../src/pjnath/turn_session.c:738: pj_turn_session_alloc: Assertion `sess->state>PJ_TURN_STATE_NULL && sess->state<=PJ_TURN_STATE_RESOLVED' failed.
Thread 63 "jami" received signal SIGABRT, Aborted.
[Switching to Thread 0x7fff32ff5640 (LWP 302307)]
__pthread_kill_implementation (no_tid=0, signo=6, threadid=140734048982592) at ./nptl/pthread_kill.c:44
44 ./nptl/pthread_kill.c: No such file or directory.
(gdb) bt
#0 __pthread_kill_implementation (no_tid=0, signo=6, threadid=140734048982592) at ./nptl/pthread_kill.c:44
#1 __pthread_kill_internal (signo=6, threadid=140734048982592) at ./nptl/pthread_kill.c:78
#2 __GI___pthread_kill (threadid=140734048982592, signo=signo@entry=6) at ./nptl/pthread_kill.c:89
#3 0x00007fffe6c42476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4 0x00007fffe6c287f3 in __GI_abort () at ./stdlib/abort.c:79
#5 0x00007fffe6c2871b in __assert_fail_base
(fmt=0x7fffe6ddd130 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=0x7fffe8eee478 "sess->state>PJ_TURN_STATE_NULL && sess->state<=PJ_TURN_STATE_RESOLVED", file=0x7fffe8eee26d "../src/pjnath/turn_session.c", line=738, function=<optimized out>) at ./assert/assert.c:92
#6 0x00007fffe6c39e96 in __GI___assert_fail
(assertion=0x7fffe8eee478 "sess->state>PJ_TURN_STATE_NULL && sess->state<=PJ_TURN_STATE_RESOLVED", file=0x7fffe8eee26d "../src/pjnath/turn_session.c", line=738, function=0x7fffe8eeeb60 <__PRETTY_FUNCTION__.10> "pj_turn_session_alloc") at ./assert/assert.c:101
#7 0x00007fffe80a3670 in pj_turn_session_alloc (sess=0x7fff7001e668, param=0x7fff7001cb00) at ../src/pjnath/turn_session.c:738
#8 0x00007fffe80a8b46 in on_connect_complete (turn_sock=0x7fff7001caa8, status=0) at ../src/pjnath/turn_sock.c:807
#9 0x00007fffe80a8bd8 in on_connect_complete_asock (asock=0x7fff7001dae0, status=0) at ../src/pjnath/turn_sock.c:827
#10 0x00007fffe80c83e2 in ioqueue_on_connect_complete (key=0x7fff7001a388, status=0) at ../src/pj/activesock.c:950
#11 0x00007fffe80be556 in ioqueue_dispatch_write_event (ioqueue=0x7fff70019d78, h=0x7fff7001a388) at ../src/pj/ioqueue_common_abs.c:290
#12 0x00007fffe80c1a2e in pj_ioqueue_poll (ioqueue=0x7fff70019d78, timeout=0x7fff32ff1600) at ../src/pj/ioqueue_epoll.c:1005
#13 0x00007fffe7ff348f in dhtnet::TurnTransport::Impl::ioJob() () at /home/fsimon/jami-client-qt/daemon/src/.libs/libjami.so.0
#14 0x00007fffe70dc253 in () at /lib/x86_64-linux-gnu/libstdc++.so.6
#15 0x00007fffe6c94ac3 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:442
#16 0x00007fffe6d26850 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81