Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in TCP connection #765

Merged
merged 6 commits into from
Feb 24, 2025
Merged

Fix memory leak in TCP connection #765

merged 6 commits into from
Feb 24, 2025

Conversation

pm47
Copy link
Member

@pm47 pm47 commented Feb 20, 2025

We instantiated a new SelectorManager at each connection attempt, leaking resources even if each socket was properly cleaned up on disconnection. The leak affects both jvm and native.

The actual fix is in the commit use a static SelectorManager.

The reformat + cleanup commit is optional. In particular it removes the startTls function that is not used anymore since we moved Tor outside phoenix (ACINQ/phoenix#662).

I've added a repro test and reverted it after the fix:

  • run the test with ./gradlew jvmTest --tests "*LeakTests*"
  • monitor resources while the test is running with watch -d "sudo lsof -p xxx | grep TCP"

Output before:

java    3284   pm   91u     IPv6              42059       0t0    TCP localhost:48202->localhost:42897 (ESTABLISHED)
java    3284   pm   96u     sock                0,7       0t0  40147 protocol: TCPv6
java    3284   pm   97u     IPv6              42066       0t0    TCP 172.31.251.88:47100->103.165.192.212:https (ESTABLISHED)
java    3284   pm   98u     sock                0,7       0t0  42071 protocol: TCPv6
java    3284   pm  101u     sock                0,7       0t0  42074 protocol: TCPv6
java    3284   pm  106u     sock                0,7       0t0  40152 protocol: TCPv6
java    3284   pm  107u     sock                0,7       0t0  36306 protocol: TCPv6
java    3284   pm  110u     sock                0,7       0t0  40153 protocol: TCPv6
java    3284   pm  114u     sock                0,7       0t0  40154 protocol: TCPv6
java    3284   pm  118u     sock                0,7       0t0  41001 protocol: TCPv6
java    3284   pm  119u     sock                0,7       0t0  40155 protocol: TCPv6
...
java    3284   pm  182u     sock                0,7       0t0 219852 protocol: TCPv6
java    3284   pm  187u     sock                0,7       0t0 218052 protocol: TCPv6
java    3284   pm  188u     sock                0,7       0t0  36409 protocol: TCPv6
java    3284   pm  191u     sock                0,7       0t0 219853 protocol: TCPv6
java    3284   pm  194u     sock                0,7       0t0  46355 protocol: TCPv6
java    3284   pm  197u     sock                0,7       0t0  40160 protocol: TCPv6
java    3284   pm  200u     sock                0,7       0t0  46356 protocol: TCPv6

Output after:

java    3718   pm   91u     IPv6              44612       0t0    TCP localhost:56804->localhost:37909 (ESTABLISHED)
java    3718   pm   95u     IPv6             196593       0t0    TCP 172.31.251.88:51150->aa323b5fc53873081.awsglobalaccelerator.com:9735 (ESTABLISHED)
java    3718   pm   98u     IPv6              46048       0t0    TCP 172.31.251.88:47344->103.165.192.212:https (ESTABLISHED)

pm47 added 5 commits February 18, 2025 14:52
We remove `startTls` which was needed for Tor connection through Socks5
proxy. It is not needed anymore since we are switching to Orbot on
phoenix.
@pm47 pm47 requested a review from sstone February 20, 2025 15:35
Copy link
Member

@sstone sstone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Just one nit: the leak test could have been added to commonTest and also used on linux ?

@pm47
Copy link
Member Author

pm47 commented Feb 24, 2025

Just one nit: the leak test could have been added to commonTest and also used on linux ?

It could, but this test was just useful for investigating easily on the JVM. I verified on native by including this change in phoenixd.

@pm47
Copy link
Member Author

pm47 commented Feb 24, 2025

The reformat + cleanup commit is optional. In particular it removes the startTls function that is not used anymore since we moved Tor outside phoenix (ACINQ/phoenix#662).

@dpad85 @robbiehanson please confirm this is okay

@dpad85
Copy link
Member

dpad85 commented Feb 24, 2025

The reformat + cleanup commit is optional. In particular it removes the startTls function that is not used anymore since we moved Tor outside phoenix (ACINQ/phoenix#662).

@dpad85 @robbiehanson please confirm this is okay

It is OK, tested on Android & iOS.

@pm47 pm47 merged commit d4017a6 into master Feb 24, 2025
2 checks passed
@pm47 pm47 deleted the tcp-close branch February 24, 2025 17:20
pm47 added a commit to ACINQ/phoenixd that referenced this pull request Feb 25, 2025
This version includes ACINQ/lightning-kmp#765 which should fix #118 and #136.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants