Title: NioClient.close() sometimes leaves the shutdown hook registered · Issue #435 · dnsjava/dnsjava · GitHub
Open Graph Title: NioClient.close() sometimes leaves the shutdown hook registered · Issue #435 · dnsjava/dnsjava
X Title: NioClient.close() sometimes leaves the shutdown hook registered · Issue #435 · dnsjava/dnsjava
Description: Version 3.7.1 (also 3.6.5; master is unchanged) Description NioClient.close() sets run = false, wakes up the selector, and only afterwards removes the shutdown hook: synchronized (NIO_CLIENT_LOCK) { run = false; localSelector = selector;...
Open Graph Description: Version 3.7.1 (also 3.6.5; master is unchanged) Description NioClient.close() sets run = false, wakes up the selector, and only afterwards removes the shutdown hook: synchronized (NIO_CLIENT_LOCK) ...
X Description: Version 3.7.1 (also 3.6.5; master is unchanged) Description NioClient.close() sets run = false, wakes up the selector, and only afterwards removes the shutdown hook: synchronized (NIO_CLIENT_LOCK) ...
Opengraph URL: https://github.com/dnsjava/dnsjava/issues/435
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"NioClient.close() sometimes leaves the shutdown hook registered","articleBody":"## Version\n\n3.7.1 (also 3.6.5; `master` is unchanged)\n\n## Description\n\n`NioClient.close()` sets `run = false`, wakes up the selector, and only afterwards removes the shutdown hook:\n\n```java\nsynchronized (NIO_CLIENT_LOCK) {\n run = false;\n localSelector = selector;\n localSelectorThread = selectorThread;\n}\nif (localSelector != null) {\n localSelector.wakeup();\n}\nif (!fromHook) {\n synchronized (NIO_CLIENT_LOCK) {\n if (closeThread != null) {\n Runtime.getRuntime().removeShutdownHook(closeThread);\n ...\n```\n\nIn between, the selector thread can leave its loop and run `cleanupAfterRun()`, which sets `closeThread = null`.\n`close()` then finds `closeThread == null` and doesn't remove the hook, so it stays registered until the JVM exits.\n\nIn a servlet container this leaks the web application: the hook thread's context class loader is the webapp's,\nso the webapp can't be unloaded after it was stopped, although `close()` was called on undeploy. This is the\nuse case of #180, which added `close()` for a clean shutdown inside Tomcat: since the self-join mentioned there\nwas fixed in 3.7.0, the selector thread stops, but the hook still keeps the webapp loaded now and then. Our\nnightly webapp leak check hit it once in about 50 runs.\n\n## Reproduction\n\nStart the client and close it in a loop, checking whether the hook is still registered afterwards. With busy\nthreads on all cores to make the closing thread lose the CPU after `wakeup()`, we got 6 leaked hooks in 2,000\niterations on a 12-core machine:\n\n```java\nField field = NioClient.class.getDeclaredField(\"closeThread\");\nfield.setAccessible(true);\nSimpleResolver resolver = new SimpleResolver(new InetSocketAddress(\"127.0.0.1\", 9));\nMessage query = Message.newQuery(Record.newRecord(Name.fromString(\"example.com.\"), Type.A, DClass.IN));\nfor (int i = 0; i \u003c 2000; i++) {\n resolver.sendAsync(query);\n Thread hook = (Thread) field.get(null);\n NioClient.close();\n if (Runtime.getRuntime().removeShutdownHook(hook)) {\n System.out.println(\"hook still registered after close()\");\n }\n}\n```\n\n## Suggested fix\n\nRead `closeThread` in the first synchronized block together with `selector` and `selectorThread`, and remove\nthat hook, or remove the hook in the same block that sets `run = false`.","author":{"url":"https://github.com/stbstb","@type":"Person","name":"stbstb"},"datePublished":"2026-11-16T09:36:15Z","url":"https://github.com/dnsjava/dnsjava/issues/435"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:06c4f7fc-7aeb-d247-4beb-240acb9274f5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A6FE:3C2607:83CF91:AA273E:6B062BBD |
| html-safe-nonce | c67ee00feadd8c43cfa3561a0c09eef9b8a0bc6e56fe7a085c2ca40c96dbca64 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNkZFOjNDMjYwNzo4M0NGOTE6QUEyNzNFOjZCMDYyQkJEIiwidmlzaXRvcl9pZCI6IjYwMzY0NzQ1MjIyNjAzNTE5MzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 1186e4c992569268c769a1af47656ad4bf2591ce6fdf435ce0257fecd5e68b7c |
| hovercard-subject-tag | issue:6418352443 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/dnsjava/dnsjava/435/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fc0fbe1c6f1f860d6ca5c334c61b66539a4d8aeb8a2a347fc3971bf82d640db8/dnsjava/dnsjava/issues/435 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fc0fbe1c6f1f860d6ca5c334c61b66539a4d8aeb8a2a347fc3971bf82d640db8/dnsjava/dnsjava/issues/435 |
| og:image:alt | Version 3.7.1 (also 3.6.5; master is unchanged) Description NioClient.close() sets run = false, wakes up the selector, and only afterwards removes the shutdown hook: synchronized (NIO_CLIENT_LOCK) ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | stbstb |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7f961de5dd5c5d4aca24d33f8b3ffb3fb8b2ef53d0a4f32f01f0a790e512f6d3 |
| turbo-cache-control | no-preview |
| go-import | github.com/dnsjava/dnsjava git https://github.com/dnsjava/dnsjava.git |
| octolytics-dimension-user_id | 6755615 |
| octolytics-dimension-user_login | dnsjava |
| octolytics-dimension-repository_id | 17084567 |
| octolytics-dimension-repository_nwo | dnsjava/dnsjava |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 17084567 |
| octolytics-dimension-repository_network_root_nwo | dnsjava/dnsjava |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 494ea0bb1e0feef734a83e2628697647755e8864 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width