letters icon indicating copy to clipboard operation
letters copied to clipboard

add finalize to close connection

Open mabiede opened this issue 7 months ago • 4 comments

Issue

Behavior: The mailer service is expected to send a lot of emails, and it does so successfully. However, it leaves many connections in the CLOSE_WAIT state, which can lead to resource exhaustion (locally with mailtrap, it stopped around 1010).

... a lot more of these lines ...
test_mail 57572 57574 test_mail opam  777u     IPv4            5117875      0t0     TCP 2ac8aaaf97d7:35134->mailtrap-letters.letters_devcontainer_default:1025 (CLOSE_WAIT)
test_mail 57572 57574 test_mail opam  778u     IPv4            5117876      0t0     TCP 2ac8aaaf97d7:35150->mailtrap-letters.letters_devcontainer_default:1025 (CLOSE_WAIT)
test_mail 57572 57574 test_mail opam  779u     IPv4            5125718      0t0     TCP 2ac8aaaf97d7:35154->mailtrap-letters.letters_devcontainer_default:1025 (CLOSE_WAIT)
test_mail 57572 57574 test_mail opam  780u     IPv4            5125719      0t0     TCP 2ac8aaaf97d7:35164->mailtrap-letters.letters_devcontainer_default:1025 (CLOSE_WAIT)
test_mail 57572 57574 test_mail opam  781u     IPv4            5128011      0t0     TCP 2ac8aaaf97d7:35174->mailtrap-letters.letters_devcontainer_default:1025 (CLOSE_WAIT)
test_mail 57572 57574 test_mail opam  782u     IPv4            5117877      0t0     TCP 2ac8aaaf97d7:35186->mailtrap-letters.letters_devcontainer_default:1025 (CLOSE_WAIT)
test_mail 57572 57574 test_mail opam  783u     IPv4            5117878      0t0     TCP 2ac8aaaf97d7:35188->mailtrap-letters.letters_devcontainer_default:1025 (CLOSE_WAIT)
test_mail 57572 57574 test_mail opam  784u     IPv4            5128012      0t0     TCP 2ac8aaaf97d7:35202->mailtrap-letters.letters_devcontainer_default:1025 (CLOSE_WAIT)
test_mail 57572 57574 test_mail opam  785u     IPv4            5130581      0t0     TCP 2ac8aaaf97d7:35206->mailtrap-letters.letters_devcontainer_default:1025 (CLOSE_WAIT)
test_mail 57572 57574 test_mail opam  786u     IPv4            5132789      0t0     TCP 2ac8aaaf97d7:35220->mailtrap-letters.letters_devcontainer_default:1025 (ESTABLISHED)

Fix

  • See changes of file: lib/sendmail_handler.ml

The following lines show the output after adjustments. No connections left with CLOSE_WAIT state. Always the same amount of connections (tested locally with 10000 mails)

test_mail 58384                 opam    1w      REG              254,1        0 2303086 ./service-test/_build/_tests/GPHM7WVI/use mailtrapU+002C test a lot of emails.000.output
test_mail 58384                 opam    2w      REG              254,1        0 2303086 ./service-test/_build/_tests/GPHM7WVI/use mailtrapU+002C test a lot of emails.000.output
test_mail 58384                 opam    4w      REG              254,1        0 2303086 ./service-test/_build/_tests/GPHM7WVI/use mailtrapU+002C test a lot of emails.000.output
test_mail 58384 58386 test_mail opam    1w      REG              254,1        0 2303086 ./service-test/_build/_tests/GPHM7WVI/use mailtrapU+002C test a lot of emails.000.output
test_mail 58384 58386 test_mail opam    2w      REG              254,1        0 2303086 ./service-test/_build/_tests/GPHM7WVI/use mailtrapU+002C test a lot of emails.000.output
test_mail 58384 58386 test_mail opam    4w      REG              254,1        0 2303086 ./service-test/_build/_tests/GPHM7WVI/use mailtrapU+002C test a lot of emails.000.output

The test is pushed to the following branch, as it's not designed to run in CI I'd keep it there: https://github.com/mabiede/letters/blob/dev/mailspam-testcase/service-test/test_mailspam.ml#L22

Additional

I did maintenance on the devcontainer setup and simplified it.

mabiede avatar Jun 12 '25 13:06 mabiede