Title: Resource Leak: SMIMESigned constructor leaves background threads hanging on MessagingException · Issue #2245 · bcgit/bc-java · GitHub
Open Graph Title: Resource Leak: SMIMESigned constructor leaves background threads hanging on MessagingException · Issue #2245 · bcgit/bc-java
X Title: Resource Leak: SMIMESigned constructor leaves background threads hanging on MessagingException · Issue #2245 · bcgit/bc-java
Description: Hi team, Description When initializing an SMIMESigned object with a MimeMessage (or MimeBodyPart), Bouncy Castle internalizes the message's content by calling getInputStream(). In many JavaMail configurations (especially with complex nes...
Open Graph Description: Hi team, Description When initializing an SMIMESigned object with a MimeMessage (or MimeBodyPart), Bouncy Castle internalizes the message's content by calling getInputStream(). In many JavaMail con...
X Description: Hi team, Description When initializing an SMIMESigned object with a MimeMessage (or MimeBodyPart), Bouncy Castle internalizes the message's content by calling getInputStream(). In many JavaMail...
Opengraph URL: https://github.com/bcgit/bc-java/issues/2245
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Resource Leak: SMIMESigned constructor leaves background threads hanging on MessagingException","articleBody":"Hi team,\n\n**Description**\nWhen initializing an SMIMESigned object with a MimeMessage (or MimeBodyPart), Bouncy Castle internalizes the message's content by calling getInputStream().\n\nIn many JavaMail configurations (especially with complex nested MimeMultipart objects), getInputStream() returns a PipedInputStream. This implementation spawns a background \"producer\" thread (often named DataHandler.getInputStream) to pump data into the pipe.\n\nIf the MimeMessage is malformed or not a valid S/MIME signed message, the SMIMESigned constructor throws a MessagingException or CMSException. However, if the input stream has already been opened, it is not explicitly closed in a finally block. Because the stream remains open but is no longer being read by the now-defunct SMIMESigned instance, the producer thread stays in a Running state indefinitely, leading to a thread leak.\n\n**Steps to Reproduce**\n1. Create a MimeMessage that is not a valid S/MIME signed message (e.g., a standard multipart/mixed message).\n1. Pass this message to the SMIMESigned constructor.\n1. Catch the resulting exception.\n1. Observe the JVM thread count or use a debugger to see that the DataHandler.getInputStream thread remains active.\n\nHere is a small java class to reproduce the issue:\n[PipedStreamThreadStuckTest.java](https://github.com/user-attachments/files/24942066/PipedStreamThreadStuckTest.java)\n\nAnd here are the threads before SMIMESigned throw exception:\n\u003cimg width=\"424\" height=\"134\" alt=\"Image\" src=\"https://github.com/user-attachments/assets/1695698b-6543-41ed-ba44-50a1205ac3a7\" />\n\n And after **Thread [DataHandler.getInputStream]** is stuck in Running state after the SMIMESigned constructor failed:\n\u003cimg width=\"443\" height=\"174\" alt=\"Image\" src=\"https://github.com/user-attachments/assets/10fac1b4-2ca3-4c85-a7b1-91285b717e89\" />\n\n**Impact**\nIn high-volume mail processing applications, this results in a gradual exhaustion of the thread pool, eventually leading to OutOfMemoryError or application instability.\n\n**Suggested Fix**\nIn the SMIMESigned (and potentially SMIMEMailCap) constructors, ensure that any InputStream obtained from a MimePart is wrapped in a try-finally block or handled such that close() is called if the parsing fails.\n\nThank you for your help!\n","author":{"url":"https://github.com/marijusGood","@type":"Person","name":"marijusGood"},"datePublished":"2026-01-29T15:55:25Z","url":"https://github.com/bcgit/bc-java/issues/2245"}
| 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:29d1e4c8-42d2-913e-8493-6b1bc6cd4e8b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E87C:5743C:19F109:244241:6AFBEF39 |
| html-safe-nonce | 264f4fec2b8881e0f90b72fc752d9ed9987979276154b51dcf7113a6bee54ae3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFODdDOjU3NDNDOjE5RjEwOToyNDQyNDE6NkFGQkVGMzkiLCJ2aXNpdG9yX2lkIjoiMzkyNzM4MDEwNzk5NTExNTMyMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d9027bfe9c92407793b3f0773418787b1abbbda174c2cf4373d1fc2d7ff1acff |
| hovercard-subject-tag | issue:3871391474 |
| 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/bcgit/bc-java/2245/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d879541276b0b5d3eddb3d10beff8f972a4cf3df7182715416cfbce93c87d80d/bcgit/bc-java/issues/2245 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d879541276b0b5d3eddb3d10beff8f972a4cf3df7182715416cfbce93c87d80d/bcgit/bc-java/issues/2245 |
| og:image:alt | Hi team, Description When initializing an SMIMESigned object with a MimeMessage (or MimeBodyPart), Bouncy Castle internalizes the message's content by calling getInputStream(). In many JavaMail con... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | marijusGood |
| hostname | github.com |
| expected-hostname | github.com |
| None | fa74413a1d84dac536fcca0740780bcbd3207bafec0e51873b73ff8e8cca032f |
| turbo-cache-control | no-preview |
| go-import | github.com/bcgit/bc-java git https://github.com/bcgit/bc-java.git |
| octolytics-dimension-user_id | 4566970 |
| octolytics-dimension-user_login | bcgit |
| octolytics-dimension-repository_id | 10416648 |
| octolytics-dimension-repository_nwo | bcgit/bc-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 10416648 |
| octolytics-dimension-repository_network_root_nwo | bcgit/bc-java |
| 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 | b3b5633a74535d9c23385512d534c66c8fde1907 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width