Skip to content

improve addShutdownHook KDoc#5548

Open
Pantus Oleh (zibet27) wants to merge 1 commit into
release/3.xfrom
zibet27/imporove-shutdown-hook-docs
Open

improve addShutdownHook KDoc#5548
Pantus Oleh (zibet27) wants to merge 1 commit into
release/3.xfrom
zibet27/imporove-shutdown-hook-docs

Conversation

@zibet27

Copy link
Copy Markdown
Collaborator

Subsystem
Server Docs

Motivation
KTOR-9504 Improve KDoc for EmbeddedServer.addShutdownHook

@coderabbitai

coderabbitai Bot commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

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: 53368199-165c-482a-a9a0-0cb3e53cf6ad

📥 Commits

Reviewing files that changed from the base of the PR and between fc08da4 and 756fec6.

📒 Files selected for processing (4)
  • ktor-server/ktor-server-core/common/src/io/ktor/server/engine/ShutdownHook.kt
  • ktor-server/ktor-server-core/jvm/src/io/ktor/server/engine/ShutdownHookJvm.kt
  • ktor-server/ktor-server-core/posix/src/io/ktor/server/engine/ShutdownHookNative.kt
  • ktor-server/ktor-server-core/web/src/io/ktor/server/engine/ShutdownHook.web.kt
✅ Files skipped from review due to trivial changes (3)
  • ktor-server/ktor-server-core/posix/src/io/ktor/server/engine/ShutdownHookNative.kt
  • ktor-server/ktor-server-core/web/src/io/ktor/server/engine/ShutdownHook.web.kt
  • ktor-server/ktor-server-core/jvm/src/io/ktor/server/engine/ShutdownHookJvm.kt

📝 Walkthrough

Walkthrough

Documentation was updated for shutdown-hook registration across common, JVM, Native, and Web sources. The revised comments describe registration timing, platform-specific listener behavior, execution order, and normal-stop versus termination-signal behavior. No logic or public signatures changed.

Changes

Shutdown hook documentation

Layer / File(s) Summary
Common shutdown-hook contract
ktor-server/ktor-server-core/common/src/io/ktor/server/engine/ShutdownHook.kt
The shared KDoc for addShutdownHook and platformAddShutdownHook now describes termination-trigger sources, registration timing, platform differences, shutdown flow, and normal-stop behavior.
Platform shutdown-hook docs
ktor-server/ktor-server-core/jvm/src/io/ktor/server/engine/ShutdownHookJvm.kt, ktor-server/ktor-server-core/posix/src/io/ktor/server/engine/ShutdownHookNative.kt, ktor-server/ktor-server-core/web/src/io/ktor/server/engine/ShutdownHook.web.kt
The JVM, Native, and Web shutdown-hook KDoc comments were updated to describe per-platform listener registration, callback retention, execution order, and at-most-once stop behavior.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The template's required Solution section is missing; only Subsystem and Motivation are provided. Add a Solution section describing the KDoc changes and how they address KTOR-9504.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and clearly matches the PR’s main change: improving addShutdownHook KDoc.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch zibet27/imporove-shutdown-hook-docs

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.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
ktor-server/ktor-server-core/common/src/io/ktor/server/engine/ShutdownHook.kt (1)

30-31: Consider refining "while the application is running" for precision.

The phrase "while the application is running" is slightly imprecise. Based on the implementation (line 38), the hook is registered when the ApplicationStarting event fires, which occurs during the startup phase rather than while the application is running.

Consider:

-The hook is only registered while the application is running. If the application has already stopped,
+The hook is registered when the application starts. If the application has already stopped,

This minor change aligns the wording more precisely with the ApplicationStarting event subscription. However, the current phrasing is understandable in context.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@ktor-server/ktor-server-core/common/src/io/ktor/server/engine/ShutdownHook.kt`
around lines 30 - 31, The comment text "while the application is running" is
imprecise; update the wording in ShutdownHook.kt to state that the hook is
registered during startup when the ApplicationStarting event fires (rather than
"while running"), and ensure the description still notes that if the application
has already stopped no hook is registered and stop will never be called;
reference the ApplicationStarting event and the stop method to make the behavior
explicit.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@ktor-server/ktor-server-core/common/src/io/ktor/server/engine/ShutdownHook.kt`:
- Around line 27-28: ShutdownHookNative.kt currently overwrites previous hooks
by assigning shutdownHook.value = ..., so addShutdownHook() on Native only keeps
the last hook; change the Native implementation to accumulate independent hooks
(e.g., store a thread-safe collection or an AtomicReference<List<() -> Unit>>
and append new callbacks, or chain the new hook to call the existing hook) so
every call to addShutdownHook() registers an independent callback and all are
executed on shutdown; additionally either implement true deregistration
semantics on Native/Web to match JVM (so EmbeddedServer.stop can remove its
hook) or update the documentation to clearly state that only the JVM platform
removes the actual shutdown hook while Native/Web use a guard to prevent
re-execution (and adjust the deregistration wording accordingly).

---

Nitpick comments:
In
`@ktor-server/ktor-server-core/common/src/io/ktor/server/engine/ShutdownHook.kt`:
- Around line 30-31: The comment text "while the application is running" is
imprecise; update the wording in ShutdownHook.kt to state that the hook is
registered during startup when the ApplicationStarting event fires (rather than
"while running"), and ensure the description still notes that if the application
has already stopped no hook is registered and stop will never be called;
reference the ApplicationStarting event and the stop method to make the behavior
explicit.
🪄 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: b97ae795-0f02-4e63-86b1-0943b38b0980

📥 Commits

Reviewing files that changed from the base of the PR and between 5d9a998 and fc08da4.

📒 Files selected for processing (1)
  • ktor-server/ktor-server-core/common/src/io/ktor/server/engine/ShutdownHook.kt

Comment thread ktor-server/ktor-server-core/common/src/io/ktor/server/engine/ShutdownHook.kt Outdated
* (JVM shutdown hook on JVM, POSIX signal on Native, process event on JS).
* Should be called **before** starting the server.
*
* Multiple shutdown hooks can be registered by calling this function more than once —

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.

What order will the callbacks be executed?

* each call adds an independent hook. For example, different application modules can each
* register their own cleanup logic.
*
* The [stop] block is **not** a listener for lifecycle events — it is the **cause** of the

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.

What is the cause of shutdown sequence?

* [io.ktor.server.application.ApplicationStopping], and
* [io.ktor.server.application.ApplicationStopped] in that order.
*
* If the application is stopped normally (e.g., by calling [EmbeddedServer.stop] directly),

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.

Is it necessary to include this in the KDoc?

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