Skip to content

Resolve the issue of inaccurate tcp_connections_no#3664

Merged
bogdan-iancu merged 2 commits into
OpenSIPS:masterfrom
36952362:tcp_connection
Jun 18, 2025
Merged

Resolve the issue of inaccurate tcp_connections_no#3664
bogdan-iancu merged 2 commits into
OpenSIPS:masterfrom
36952362:tcp_connection

Conversation

@36952362
Copy link
Copy Markdown
Contributor

@36952362 36952362 commented Jun 4, 2025

Summary
When OpenSIPS acts as a TCP/TLS client to create connections, the tcp_connections_no counter gradually decreases and may even become negative.

Details
When other modules, such as tm, need to send a message to a target address and no existing TCP/TLS connection is available, the TCP worker process responsible for sending the message will attempt to establish a new TLS connection and create a tcp_connection object. It also increments the tcp_connections_no counter; however, this increment only affects the tcp_connections_no variable local to the TCP worker process (as this variable is process-specific). Then, the worker notifies the TCP main process via a command (either ASYNC_CONNECT or CONN_NEW).

proto_tls_send
  --> tcp_async_connect
  		--> tcp_conn_create
  		      --> tcpconn_new
  		      		--> tcp_connections_no++;
  		      --> tcp_conn_send

However, when the TCP main process receives the ASYNC_CONNECT or CONN_NEW command, it does not increment its own tcp_connections_no counter. Later, when the connection is destroyed (in tcpconn_destroy) or expires due to its lifetime (in tcpconn_lifetime), the counter is decremented.

As a result, the tcp_connections_no counter gradually decreases and may even become negative.
create_new_connection

Solution
when the TCP main process receives the ASYNC_CONNECT or CONN_NEW command, it also increment its own tcp_connections_no counter

Compatibility
N/A

Closing issues
N/A

@bogdan-iancu
Copy link
Copy Markdown
Member

@36952362 , this change is already part of #3654, which I think is by mistake - do you want to remove it from there and keep the 2 issues with its own PR ?

@bogdan-iancu
Copy link
Copy Markdown
Member

Thank you @36952362 for the detailed report - you are totally right here. The tcp_connections_no counter from non TCP MAIN procs must not be touched (it's useless). Only TCP MAIN works with it. So the fix should contain two parts:

  1. do not increment in a non TCP-MAIN proc - maybe adding one more param to tcpconn_new() (if the increment should be done or not) and have that param correlated with the send2main param of tcp_conn_create()
  2. do the increment when the conn is received by the TCP MAIN upon ASYNC_CONNECT or CONN_NEW command (this is already done in this PR)

On a closer look at this tcp_connections_no, it seems to be used to only limit the overall number of TCP conns. But this is a bit bogus as the test is done only upon accepting new connections, it is not done doing a client connect....so I doubt a bit about its overall value.

@36952362
Copy link
Copy Markdown
Contributor Author

Thanks for your comments @bogdan-iancu .I had updated the PR according to your suggestion. add one more param(in_main_proc) to indicate this new tcp connection will be created in TCP MAIN proc or not. Please help to double check the logic again. thanks

Agree with you. According to the comment of tcp_connections_no, it means "current number of open connections", but it only checks for new in-coming connections, it does not accurately.

@bogdan-iancu bogdan-iancu merged commit 38efd08 into OpenSIPS:master Jun 18, 2025
86 checks passed
bogdan-iancu added a commit that referenced this pull request Jun 18, 2025
Resolve the issue of inaccurate tcp_connections_no

(cherry picked from commit 38efd08)
bogdan-iancu added a commit that referenced this pull request Jun 18, 2025
Resolve the issue of inaccurate tcp_connections_no

(cherry picked from commit 38efd08)
bogdan-iancu added a commit that referenced this pull request Jun 18, 2025
Resolve the issue of inaccurate tcp_connections_no

(cherry picked from commit 38efd08)
@bogdan-iancu
Copy link
Copy Markdown
Member

Thank you @36952362 , merge done and backported to all 3.4+ versions

@36952362 36952362 deleted the tcp_connection branch June 19, 2025 06:25
NormB pushed a commit to NormB/opensips that referenced this pull request May 9, 2026
Resolve the issue of inaccurate tcp_connections_no
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants