Skip to content

KTOR-7760 Clean up resources when bind fails with non-BindException#5587

Merged
bjhham merged 2 commits into
ktorio:mainfrom
fru1tworld:fix/7760-bind-failure-cleanup
May 13, 2026
Merged

KTOR-7760 Clean up resources when bind fails with non-BindException#5587
bjhham merged 2 commits into
ktorio:mainfrom
fru1tworld:fix/7760-bind-failure-cleanup

Conversation

@fru1tworld
Copy link
Copy Markdown
Contributor

Subsystem
Server, Netty

Motivation
KTOR-7760 Server finishes working without an exception when no privileges to open the port

NettyApplicationEngine.start() only catches BindException. Other bind failures (SocketException from Linux NIO, Errors.NativeIoException from epoll, IllegalArgumentException for invalid ports) propagate uncaught while the already-initialized Netty event loop groups stay alive on non-daemon threads, so the process hangs or appears to exit silently instead of surfacing the error.

Solution
Add a catch (Throwable) branch that calls stop(0, 0) before rethrowing, mirroring the existing BindException cleanup path. New test (start cleans up resources on non-BindException failure) triggers an out-of-range port to assert the event loop groups are terminated after a non-BindException failure.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 8, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4365225e-59e0-41f8-bc33-5f9d24645868

📥 Commits

Reviewing files that changed from the base of the PR and between 5a645c0 and e55b321.

📒 Files selected for processing (1)
  • ktor-server/ktor-server-netty/jvm/src/io/ktor/server/netty/NettyApplicationEngine.kt
🚧 Files skipped from review as they are similar to previous changes (1)
  • ktor-server/ktor-server-netty/jvm/src/io/ktor/server/netty/NettyApplicationEngine.kt

📝 Walkthrough

Walkthrough

NettyApplicationEngine.start(wait) now catches any Throwable during channel binding and calls terminate() to ensure resource cleanup before rethrowing. A new test verifies that non-BindException failures also trigger ExecutorService termination.

Changes

Resource Cleanup on Non-BindException Failures

Layer / File(s) Summary
Engine Shutdown Handler
ktor-server/ktor-server-netty/jvm/src/io/ktor/server/netty/NettyApplicationEngine.kt
Replaced catch (BindException) with catch (Throwable) in start(wait), call terminate() on failure, then rethrow; removed unused BindException import.
Test Verification
ktor-server/ktor-server-netty/jvm/test/io/ktor/tests/server/netty/NettySpecificTest.kt
Added test start cleans up resources on non-BindException failure that starts embeddedServer(Netty, 70000), expects IllegalArgumentException, and asserts Netty bootstrap groups' ExecutorService instances are terminated.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • ktorio/ktor#5230: Both PRs modify NettyApplicationEngine in ktor-server-netty and affect engine startup/shutdown behavior.

Suggested reviewers

  • bjhham
  • osipxd
  • marychatte
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: resource cleanup when bind fails with exceptions other than BindException.
Description check ✅ Passed The description follows the required template with all sections (Subsystem, Motivation, Solution) properly filled out with relevant details and context.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
ktor-server/ktor-server-netty/jvm/test/io/ktor/tests/server/netty/NettySpecificTest.kt (1)

76-79: ⚡ Quick win

Assertion only checks the boss group — child group termination is not verified

it.config().group() returns only the connectionEventGroup (boss group). Since stop(0, 0) also shuts down workerEventGroup (child group), verifying both would give stronger coverage and align with the motivation for using stop() over terminate() (which omits workerEventGroup).

✅ Proposed assertion expansion
 assertTrue(
-    server.engine.bootstraps.all { (it.config().group() as ExecutorService).isTerminated },
+    server.engine.bootstraps.all {
+        (it.config().group() as ExecutorService).isTerminated &&
+            (it.config().childGroup() as ExecutorService).isTerminated
+    },
     "event loop groups must be terminated when bind fails with a non-BindException"
 )
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@ktor-server/ktor-server-netty/jvm/test/io/ktor/tests/server/netty/NettySpecificTest.kt`
around lines 76 - 79, The current assertion only checks the boss
(connectionEventGroup) via server.engine.bootstraps and it.config().group();
update the assertion to verify both boss and worker (child) event loop groups
are terminated when bind fails: for each bootstrap in server.engine.bootstraps,
check that (it.config().group() as ExecutorService).isTerminated AND the
child/worker event group (access the bootstrap's child group, e.g.
it.config().childGroup() or equivalent API that returns the workerEventGroup) is
also terminated, and include a clear assertion message about verifying both
groups.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@ktor-server/ktor-server-netty/jvm/src/io/ktor/server/netty/NettyApplicationEngine.kt`:
- Around line 260-266: The catch-all startup handler currently calls stop(0, 0)
which fires ApplicationStopPreparing and may mask the original exception; change
the catch (Throwable) path in start() to call terminate() instead of stop(0, 0)
and update terminate() to also shut down workerEventGroup (in addition to
bossGroup/workerGroup) so terminate() performs the full, silent shutdown used on
BindException; ensure both the BindException and generic Throwable branches call
terminate() so lifecycle events are not raised during failed startup.

---

Nitpick comments:
In
`@ktor-server/ktor-server-netty/jvm/test/io/ktor/tests/server/netty/NettySpecificTest.kt`:
- Around line 76-79: The current assertion only checks the boss
(connectionEventGroup) via server.engine.bootstraps and it.config().group();
update the assertion to verify both boss and worker (child) event loop groups
are terminated when bind fails: for each bootstrap in server.engine.bootstraps,
check that (it.config().group() as ExecutorService).isTerminated AND the
child/worker event group (access the bootstrap's child group, e.g.
it.config().childGroup() or equivalent API that returns the workerEventGroup) is
also terminated, and include a clear assertion message about verifying both
groups.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ae2cc19f-2b88-434a-88ad-a78526f3a347

📥 Commits

Reviewing files that changed from the base of the PR and between 2413814 and 5a645c0.

📒 Files selected for processing (2)
  • ktor-server/ktor-server-netty/jvm/src/io/ktor/server/netty/NettyApplicationEngine.kt
  • ktor-server/ktor-server-netty/jvm/test/io/ktor/tests/server/netty/NettySpecificTest.kt

Comment on lines 260 to 266
} catch (cause: BindException) {
terminate()
throw cause
} catch (cause: Throwable) {
stop(0, 0)
throw cause
}
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot May 8, 2026

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

stop(0, 0) fires lifecycle events on a never-started server and can mask the root cause

Two problems with using stop(0, 0) in the new catch (Throwable) path instead of terminate():

  1. Lifecycle event misfiring: stop() unconditionally calls monitor.raise(ApplicationStopPreparing, environment). ApplicationStopPreparing is semantically meaningful only for a running server. Firing it during a failed startup means any user-registered listener receives this event even though ApplicationStarted/ServerReady was never raised, potentially dereferencing components that were never initialized.

  2. Original exception can be masked: monitor.raise() inside stop() is not guarded by withStopException. If a listener throws, that exception propagates out of stop() and the throw cause on line 265 is never reached — the original bind failure is lost.

The BindException path uses terminate() which avoids both problems (no lifecycle events, targeted shutdown). The simplest consistent fix is to reuse terminate() here, while also addressing the pre-existing gap in terminate() — it currently omits workerEventGroup. A clean resolution would be to add workerEventGroup shutdown to terminate() and use it in both branches:

🛠️ Proposed fix
 private fun terminate() {
     withStopException {
         connectionEventGroup.shutdownGracefully().sync()
     }
+    withStopException {
+        workerEventGroup.shutdownGracefully().sync()
+    }
     withStopException {
         callEventGroup.shutdownGracefully().sync()
     }
 }

Then in start():

-    } catch (cause: Throwable) {
-        stop(0, 0)
-        throw cause
-    }
+    } catch (cause: Throwable) {
+        terminate()
+        throw cause
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} catch (cause: BindException) {
terminate()
throw cause
} catch (cause: Throwable) {
stop(0, 0)
throw cause
}
} catch (cause: BindException) {
terminate()
throw cause
} catch (cause: Throwable) {
terminate()
throw cause
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@ktor-server/ktor-server-netty/jvm/src/io/ktor/server/netty/NettyApplicationEngine.kt`
around lines 260 - 266, The catch-all startup handler currently calls stop(0, 0)
which fires ApplicationStopPreparing and may mask the original exception; change
the catch (Throwable) path in start() to call terminate() instead of stop(0, 0)
and update terminate() to also shut down workerEventGroup (in addition to
bossGroup/workerGroup) so terminate() performs the full, silent shutdown used on
BindException; ensure both the BindException and generic Throwable branches call
terminate() so lifecycle events are not raised during failed startup.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems correct @fru1tworld - we can just generalize the catch(cause: BindException) to catch(cause: Throwable) for all startup failures.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Copy Markdown
Contributor

@bjhham bjhham left a comment

Choose a reason for hiding this comment

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

Looks like terminate would be more appropriate here.

Comment on lines 260 to 266
} catch (cause: BindException) {
terminate()
throw cause
} catch (cause: Throwable) {
stop(0, 0)
throw cause
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems correct @fru1tworld - we can just generalize the catch(cause: BindException) to catch(cause: Throwable) for all startup failures.

@fru1tworld
Copy link
Copy Markdown
Contributor Author

Thanks for the review! Merged the catch blocks. 😄

@bjhham bjhham enabled auto-merge (squash) May 13, 2026 05:51
@bjhham bjhham merged commit 1cb4cf1 into ktorio:main May 13, 2026
19 checks passed
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.

2 participants