clean resources (sockets/files)#5335
Conversation
fa49449 to
d46e904
Compare
Code ReviewOverview: This PR addresses a real resource-leak / DoS vector: when a request body exceeds the configured max length while being streamed to a temp file, the partially-written file was left on disk. The fix adds Strengths
Suggestions1. Narrow exception scope leaves other leaks (most important). Only the max-length exception triggers cleanup. Any other mid-stream failure (connection reset, client abort, I/O error) still leaves the temp file behind — the same concern, different trigger. Consider cleaning up on any failure of 2. netty-cats: unidiomatic .handleError {
case e: StreamMaxLengthExceededException => file.delete(); throw e
case th => throw th
}
.onError { case _: StreamMaxLengthExceededException => Sync[F].delay { file.delete(); () } }3. Side effects not suspended (FP). In the cats and zio handlers, 4. Ignored 5. No test coverage. The fix adds no test verifying the file is actually removed after a limit breach. A deterministic assertion (send oversized body → assert temp dir has no leftover file) would lock in the behavior and guard against regression. 6. Unrelated noise mixed in. Cosmetic changes ride along ( RiskLow functional risk — changes are additive on existing error paths; the happy path is untouched. Main residual gap is that cleanup is exception-specific, so temp files from non-size-limit failures remain a leak. Verdict: Solid, well-targeted fix applied consistently. Before merge I'd suggest: (a) broaden cleanup to all failure modes (or note why only the size limit matters), (b) switch netty-cats to |
|
For file deletion the method |
As a result of investigating the unstable tests, the following issues were discovered:
netstat -nap | grep tcp | grep
ps ax | grep sbt | grep java | awk '{print $1}'tcp6 32 0 127.0.0.1:36046 127.0.0.1:39005 CLOSE_WAIT 28290/java
tcp6 0 0 127.0.0.1:52832 127.0.0.1:41039 CLOSE_WAIT 28290/java
tcp6 0 0 127.0.0.1:37852 127.0.0.1:33087 CLOSE_WAIT 28290/java
Tested on ubuntu 25 and slackware 15.
But it failed unpredictably - so solution was removed from this PR
After adding ServerWebSocketTests.scala waitForMessage the problem disappeared.