Repository navigation
Fix reactive resource leaks in blob stores, postgres table manager, message storer and unacked message hanging in RabbitMQ dequeuer - #3238
prosgarz35 wants to merge 1 commit into
Conversation
| Connection::close, | ||
| (connection, error) -> connection.close(), |
There was a problem hiding this comment.
Changes in this class is unrelated
chibenwa
left a comment
There was a problem hiding this comment.
The premise is incorrect: the 3-argument Mono.usingWhen(resource, closure, asyncCleanup) already applies asyncCleanup on completion, error and cancellation (it delegates to the
5-argument variant with the same function). All usingWhen changes here (S3, Store, AES, Zstd, MessageStorer, PostgresTableManager) are therefore no-ops and should be dropped. The
same applies to #3237 and #3239.
The Dequeuer change is valid, but for a different reason than the one described: today a load timeout errors the whole deQueue() flux (which closes the receiver) rather than
leaving deliveries unacked. Moving the timeout before onErrorResume turns it into a per-message nack/requeue. Could you reduce the PR to that change, add a test (e.g. a MailLoader that never emits), and open a JIRA ticket?
Summary of Changes
This PR addresses several critical resource leak vulnerabilities and queue-stall scenarios in both
distributed-appandpostgres-app:Reactor
Mono.usingWhenResource Leaks on Error and Cancellation:In Project Reactor 3,
Mono.usingWhen(resourceSupplier, resourceClosure, asyncCleanup)only triggersasyncCleanupwhen the publisher completes successfully. If the reactive stream terminates with an error (onError) or is cancelled by a downstream consumer (onCancel), the acquired resource is never released unless the 4-arg or 5-argusingWhenvariants provide explicit cleanup callbacks.This caused unclosed file descriptors, network streams, temporary disk files, and R2DBC database connections across multiple core modules:
S3BlobStoreDAO:InputStreamfor S3 upload was left open when an S3 upload errored or was cancelled.Store(blob-common): Inbound HTTP/Blob responseInputStreamremained unclosed when reactive byte reading errored or cancelled.AESBlobStoreDAO&ZstdBlobStoreDAO: TemporaryFileBackedOutputStreambuffers were not reset/deleted upon compression/encryption errors or cancellations.MessageStorer: Temporary attachment files (parsingResults.dispose()) were not cleaned up when attachment persistence was interrupted or cancelled.PostgresTableManager: R2DBC connections acquired during extension initialization, table migration, listing, truncation, and index initialization were not returned to the connection pool if an error occurred or downstream cancelled.RabbitMQ Un-acked Queue Stall on Timeout (
Dequeuer.java):In
RabbitMQMailQueue.Dequeuer,.timeout(TIMEOUT)was placed downstream of.onErrorResume(...). When blob store fetching stalled and timed out, Reactor threw aTimeoutExceptionafter the error handling stage. As a result, the RabbitMQ delivery was never rejected/nacked, leaving unacknowledged deliveries indefinitely holding worker slots.Placing
.timeout(TIMEOUT)upstream of.onErrorResume(...)ensures any timeout is caught and properly acknowledged/nacked with consumer backpressure.Detailed Impact & Fixes
1.
server/queue/queue-rabbitmq-Dequeuer.javaorg.apache.james.queue.rabbitmq.Dequeuer#dequeueTimeoutExceptionthat bypassed the error-handling and dead-lettering pipeline..timeout(TIMEOUT)before.onErrorResume(...)so timeouts properly trigger message requeue or nack.2.
server/blob/blob-s3-S3BlobStoreDAO.javaorg.apache.james.blob.objectstorage.aws.S3BlobStoreDAO#saveByteSource.openStream()insideMono.usingWhenlackedonErrorandonCancelrollbacks. Network disruptions during S3 streaming causedInputStream/ socket leaks.(stream, error)andstreamcleanup functions toMono.usingWhen.3.
server/blob/blob-common-Store.javaorg.apache.james.blob.api.Store#readByteSourceblobStore.readReactive(...)inusingWhenleaked response streams on downstream subscriber cancellation or read errors.onErrorandonCancelstream closure handlers.4.
server/blob/blob-aes&server/blob/blob-zstdAESBlobStoreDAO#save,ZstdBlobStoreDAO#save,ZstdBlobStoreDAO#compressAndSaveFileBackedOutputStreaminstances were only reset upon successful completion, accumulating temporary files in temp storage on failed operations.(buffer, error)andbuffercleanup functions to ensurebuffer.reset()is always called on boundedElastic scheduler.5.
mailbox/store-MessageStorer.javaorg.apache.james.mailbox.store.MessageStorer#storeAttachmentsParsedAttachments) were not disposed if saving metadata to attachment mapper failed or was cancelled.onErrorandonCancel.6.
backends-common/postgres-PostgresTableManager.javainitializePostgresExtension,initializeTables,listExistTables,truncate,initializeTableIndexesonErrorandonCancelconnection close handlers.Verification
All affected modules as well as complete application targets were built and verified with Java 21:
backends-common/postgres:mvn test-compile- SUCCESSserver/blob/blob-api,server/blob/blob-common,server/blob/blob-s3,server/blob/blob-aes,server/blob/blob-zstd:mvn test-compile- SUCCESSmailbox/store:mvn test-compile- SUCCESSserver/queue/queue-rabbitmq:mvn test-compile- SUCCESSserver/apps/distributed-app:mvn test-compile- SUCCESSserver/apps/postgres-app:mvn test-compile- SUCCESS