Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-15852

WebAsyncManager should cancel task thread on timeout

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 4.3.5
    • Fix Version/s: 4.3.11, 5.0 RC4
    • Component/s: Web
    • Labels:
      None

      Description

      Hi,

      We are implementing a REST service with and endpoint that does some processing, part of it being in a database transaction. We would like to use WebAsyncTask to run this task in a separate thread and establish a timeout. However, we find that when the timeout has elapsed the service returns the corresponding Timeout Exception but the thread continues to work finishing the database transaction while we would like it to fail and perform a rollback.

      We understand that the way to control it is by honoring isInterrupted() inside our code before committing the transaction. This way the executor thread will know that the timeout has exceeded and will exit with an exception performing a rollback. However it seems that the thread is not interrupted.

      Which is the right way to do this? Is the spring platform supposed to call .interrupt() when the timeout is activated or we have to do this ourselves?

      Thanks,
      Jordi.

        Activity

        Hide
        juergen.hoeller Juergen Hoeller added a comment - - edited

        Indeed, interruption is the way to go here. We can't interrupt the task thread directly since we're dispatching to it behind an Executor abstraction; however, we should be able to cancel the returned Future with cancel(true) which leads to interruption of the target thread.

        Show
        juergen.hoeller Juergen Hoeller added a comment - - edited Indeed, interruption is the way to go here. We can't interrupt the task thread directly since we're dispatching to it behind an Executor abstraction; however, we should be able to cancel the returned Future with cancel(true) which leads to interruption of the target thread.
        Hide
        jpuigsegur Jordi Puigsegur added a comment -

        Ok. Thanks. I'll keep an eye on this thread.
        So far we have implemented the following workaround: The runner callable stores the thread in a final array which is shared with the timeout callable. The timeout callable then can interrupt the thread on the timeout. When this is solved we will be able to remove this code.

        Show
        jpuigsegur Jordi Puigsegur added a comment - Ok. Thanks. I'll keep an eye on this thread. So far we have implemented the following workaround: The runner callable stores the thread in a final array which is shared with the timeout callable. The timeout callable then can interrupt the thread on the timeout. When this is solved we will be able to remove this code.
        Hide
        rstoya05-aop Rossen Stoyanchev added a comment -

        I've added a call to cancel the Future. Feel free to give it a try to confirm it works in your scenario.

        Show
        rstoya05-aop Rossen Stoyanchev added a comment - I've added a call to cancel the Future. Feel free to give it a try to confirm it works in your scenario.
        Hide
        jpuigsegur Jordi Puigsegur added a comment -

        Thanks,
        As soon as we can we will try to test it and confirm it works.

        Show
        jpuigsegur Jordi Puigsegur added a comment - Thanks, As soon as we can we will try to test it and confirm it works.

          People

          • Assignee:
            rstoya05-aop Rossen Stoyanchev
            Reporter:
            jpuigsegur Jordi Puigsegur
            Last updater:
            St├ęphane Nicoll
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since last comment:
              5 weeks, 3 days ago