Repository navigation
Prevent RabbitMQ channel pool starvation, event bus listener leak, and webadmin resource leaks on cancellation and error - #3239
prosgarz35 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Note that the 3-argument Mono.usingWhen(resource, closure, asyncCleanup) already applies asyncCleanup on completion, error and cancellation (it delegates to the 5-argument variant with that same function for all three).
The changes in MailReceptionCheck, ExportService and RestoreService are therefore no-ops, and the leaks
described don't occur. The only meaningful change is in ReactorRabbitMQChannelPool.tryChannel(): invalidating a closed channel instead of releasing it, plus the null guard.
Could you reduce the PR to that, ideally sharing the logic with getChannelCloseHandler() and adding a test?
|
Hello @prosgarz35
Would you have time to rework this PR in order to address the only real issue mentionned here? Thanks, Best regards, Benoit |
Overview
This PR fixes multiple high-severity resource leak vulnerabilities across RabbitMQ connection pooling, health checks, and WebAdmin maintenance routes:
RabbitMQ Channel Pool Starvation (
ReactorRabbitMQChannelPool.java):tryChannel(), invoked byRabbitMQHealthCheckperiodically, borrowed a channel from the reactive channel pool viaborrow().Mono.usingWhen(borrow(), channel -> Mono.just(channel.isOpen()), ...)only the success path returned the borrowed channel back to the pool (pooledRef.release()).refs.Timeout waiting for idle object), completely blocking queue publishing and dequeuing.onErrorandonCancelcallbacks to release or invalidate the channel.EventBus Listener Memory Leak on HealthCheck Timeout (
MailReceptionCheck.java):MailReceptionCheck.check(), a temporaryAwaitReceptionListenerwas registered on theeventBus..timeout(...)), the reactive stream was cancelled, butRegistration::unregisterwas only hooked intoonComplete.eventBus, causing memory bloat and degrading event dispatch performance.(registration, error) -> registration.unregister()andRegistration::unregisterfor cancellation cleanup.Temporary Blob Leak during Mailbox Export (
ExportService.java):ExportService.export(), user mailboxes are zipped and temporarily staged in the BlobStore (blobStore.save(...)).deleteBlobwas only executed upon successful export completion. If the export failed or was cancelled via WebAdmin Tasks API, the temporary blob was never deleted, leaving orphan blobs in S3 / file / Cassandra storage.deleteBlobis always executed.Stream & File Descriptor Leak during Message Vault Restore (
RestoreService.java):RestoreService.appendToMailbox(), nestedMono.usingWhencalls handled the MIMEInputStreamandByteSourceContent.usingWhencalls closed resources on error or cancellation.onErrorandonCancelclose handlers to bothusingWhenblocks.Changes by Module
backends-common/rabbitmq:org.apache.james.backends.rabbitmq.ReactorRabbitMQChannelPool:releaseChannel(Channel)that checks channel status and executespooledRef.release()orpooledRef.invalidate().releaseChanneltoonErrorandonCancelintryChannel().server/container/feature-checks:org.apache.james.healthcheck.MailReceptionCheck:eventBuslistener.server/protocols/webadmin/webadmin-mailbox:org.apache.james.webadmin.service.ExportService:server/protocols/webadmin/webadmin-mailbox-deleted-message-vault:org.apache.james.webadmin.vault.routes.RestoreService:InputStreamand theByteSourceContenttemporary wrapper.Verification
The fixes were compiled and verified on JDK 21:
backends-common/rabbitmq:mvn test-compile- SUCCESSserver/container/feature-checks:mvn test-compile- SUCCESSserver/protocols/webadmin/webadmin-mailbox:mvn test-compile- SUCCESSserver/protocols/webadmin/webadmin-mailbox-deleted-message-vault:mvn test-compile- SUCCESSserver/apps/distributed-app:mvn test-compile- SUCCESSserver/apps/postgres-app:mvn test-compile- SUCCESS