Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-5562

Failure to propagate exceptions in Async API

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 5.1.4, 5.2.0
    • Affects Version/s: 5.1.0
    • Component/s: Async
    • None
    • Java Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      An issue has been identified in the asynchronous API where exceptions thrown in plain code during thenRun operations are not properly propagated to the completion callback. 

      The issue arises when we dispatch to an asynchronous method in a separate thread within thenRun, and in the subsequent thenRun, we execute plain code that can throw an exception. Exceptions thrown are not caught and passed to the completion callback, but instead escalate up the thread's exception handler, leaving the callback uninvoked.

      Example Code:

      beginAsync().thenRun(c -> {
          new Thread() {
              public void run() {
                  c.complete(c);
              }
          }.start();
      }).thenRun(callback -> {
          throw new RuntimeException("Error");
      }).finish(userCallback);
      

      Expected Result: The userCallback should be invoked with the thrown exception, indicating that an exception occurred.

      Actual Result: userCallback is not called, and the exception is unhandled, propagating up to the thread handler instead.

      Suggested fix: Modify the Async API to use finish instead of unsafeFinish to include error handling that ensures exceptions in the chain are captured and passed to the completion callback when are executed on a different thread.

            Assignee:
            slav.babanin@mongodb.com Slav Babanin
            Reporter:
            slav.babanin@mongodb.com Slav Babanin
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: