Skip to content

pjnath: Leak of UDP socket used by TURN relay

Problem

When TURN.enable is set to true, then upon creation of a ICE transport using pj_ice_strans_create(), multiple UDP sockets will be opened by PJSIP. However, 4 of these sockets are never closed by pj_ice_strans_destroy(). These sockets are all allocated from the same place.

Here:

/ring-project/daemon/test/agent/agent(pj_sock_socket+0x76)[0x55555b78e260]                                                                                                                                                                                     
/ring-project/daemon/test/agent/agent(turn_on_state)[0x55555b7740f9]                                                                                                                                                                                              
/ring-project/daemon/test/agent/agent(set_state)[0x55555b76d6cb]                                                                                                                                                                                              
/ring-project/daemon/test/agent/agent(pj_turn_session_set_server+0x51d)[0x55555b76e14b]                                                                                                                                                                        
/ring-project/daemon/test/agent/agent(pj_turn_sock_alloc+0x1a3)[0x55555b772b95]                                                                                                                                                                                
/ring-project/daemon/test/agent/agent(add_update_turn)[0x55555b757d00]                                                                                                                                                                                              
/ring-project/daemon/test/agent/agent(create_comp)[0x55555b7597f5]                                                                                                                                                                                              
/ring-project/daemon/test/agent/agent(pj_ice_strans_create+0x405)[0x55555b759e24] 

NOTE! When TURN.enable is set to false there's no leak.

How to reproduce

  1. Run one passive agent.
  2. Run one active agent with scenario/call:periodic to the passive agent.
  3. Check leak with watch -n 0.1 'lsof -p PID | grep UDP | wc -l' where PID is the PID of the active agent.

How to trace UDP leak

  1. Compile trace-udp.c with gcc -O0 -ggdb3 -Wall -shared -fPIC trace-udp.c -o libtrace-udp.so -ldl
  2. Compile the agents by adding -rdynamic to AM_CXXFLAGS in Makefile.am.
  3. Run the active agent with LD_PRELOAD=./libtrace-udp.so.
  4. Check UDP allocations under udp-trace.txt and match with leaks found.
Edited by Olivier Dion