dajiaohuang opened a new pull request, #4319:
URL: https://github.com/apache/logging-log4j2/pull/4319

   Fixes #4318.
   
   `AbstractDatabaseManager#flush()` handed the buffered events to
   `writeInternal()` and then ran `commitAndClose()` and `buffer.clear()` in
   the same `finally` block:
   
   ```java
   } finally {
       this.commitAndClose();
       // not sure if this should be done when writing the events failed
       this.buffer.clear();
   }
   ```
   
   When `commitAndClose()` throws (a database failure during the commit/close
   phase), the `clear()` was skipped. The events therefore stayed in the
   buffer and were written a second time by the following flush, and, because
   the buffer keeps accumulating new events while the failure persists, it
   grew without bound. `flush()` is also called from `shutdown()`, so a
   failing commit at shutdown could also mask the original exception with a
   secondary one from re-sending the same events.
   
   The fix moves `buffer.clear()` into an inner `finally` block, so the events
   are discarded no matter how `commitAndClose()` returns or throws. The
   events were already passed to the database layer, so keeping them cannot
   recover the transaction.
   
   `testBufferedEventsAreDiscardedWhenCommitFails` makes `commitAndClose()`
   fail once and then succeed, and asserts that `writeInternal()` is invoked
   exactly twice in total (once per event), i.e. that the failed transaction
   is not re-sent.
   
   Note on scope: `connectAndStart()` is deliberately left outside the `try`,
   so events are still retried when the *connection* cannot be established.
   Only the commit/close phase changes behaviour here.
   
   ## Checklist
   
   * Base your changes on `2.x` branch if you are targeting Log4j 2; use `main` 
otherwise
   * `./mvnw verify` succeeds ([the build 
instructions](https://logging.apache.org/log4j/2.x/development.html#building))
   * Non-trivial changes contain an entry file in the `src/changelog/.2.x.x` 
directory
   * Tests are provided
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to