Giter Club home page Giter Club logo

Comments (5)

bbyk avatar bbyk commented on July 4, 2024 1

Thanks @tsegismont ! Hopefully the last thing along these lines: I think you might be missing the following override in the new class ContextCoroutineDispatcher.

  override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle {
    return (delegate as Delay).invokeOnTimeout(timeMillis, block, context)
  }

With the override the timeout functionality will run with io.vertx.kotlin.coroutines.VertxCoroutineExecutor#schedule(java.lang.Runnable, long, java.util.concurrent.TimeUnit)

While without the override, it will rely on DefaultDelay.invokeOnTimeout(timeMillis, block, context) which is a different executor which gets involved.

For example, with the override and the code like this

      try {
        withTimeout(100) {
          delay(1000)
        }
      } catch (e: TimeoutCancellationException) {

you will get
image

while without the override it's

image

The issue doesn't change correctness, to be sure, but users might expect the functionality to stay exclusively with the Vert.x threads

from vertx-lang-kotlin.

tsegismont avatar tsegismont commented on July 4, 2024 1

@bbyk Thanks for you thorough review

I've tried to reproduce but the test passes:

@Test
  fun `test withTimeout execution dispatched on Vertx coroutine executor`(testContext: TestContext) {
    val latch = testContext.async()
    val context = (vertx as VertxInternal).getOrCreateContext()
    val duplicatedContext = context.duplicate()
    val promise = Promise.promise<Any>()
    duplicatedContext.runOnContext {
      GlobalScope.launch(vertx.dispatcher()) {
        try {
          withTimeout(100) {
            promise.future().await()
          }
        } catch (e: TimeoutCancellationException) {
          testContext.assertEquals(ContextInternal.current(), duplicatedContext)
          latch.complete()
        }
      }
    }
  }

Can you share your reproducer?

from vertx-lang-kotlin.

tsegismont avatar tsegismont commented on July 4, 2024 1

I'd like to mention again that the missing override doesn't change correctness of withTimeout, it only leads to spawning a non-Vert.x thread for running timeouts which, I assume, is less desirable.

Got you. This will be fixed by #246

from vertx-lang-kotlin.

tsegismont avatar tsegismont commented on July 4, 2024

Fixed by #244

from vertx-lang-kotlin.

bbyk avatar bbyk commented on July 4, 2024

@tsegismont I am sharing it below for a different concurrency primitive. But first I'd like to mention again that the missing override doesn't change correctness of withTimeout, it only leads to spawning a non-Vert.x thread for running timeouts which, I assume, is less desirable. You can put a breakpoint in kotlinx.coroutines.TimeoutCoroutine#run to see what I see on the screenshot.

You can have a more contrived case, though, that actually fails in a test. E.g. the following test fails ( a kludge: when you paste the sketched code, please remove private access modifier from VertxCoroutineExecutor for the sake of the test run - I didn't feel like re-implementing it in-place ). If you un-comment the code in invokeOnTimeout the test will start to pass.

  @InternalCoroutinesApi
  @Test
  fun `test select-onTimeout execution dispatched on Vertx coroutine executor`(testContext: TestContext) {
    val latch = testContext.async()
    val context = (vertx as VertxInternal).getOrCreateContext()
    val duplicatedContext = context.duplicate()
    val invokeTimeoutOnDuplicatedContextDispatcher = object : CoroutineDispatcher(), Delay {
      override fun dispatch(context: CoroutineContext, block: Runnable) {
        testContext.fail()
      }

      override fun isDispatchNeeded(context: CoroutineContext): Boolean {
        return false
      }

      override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation<Unit>) {
        testContext.fail()
      }

      override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle {
        return super.invokeOnTimeout(timeMillis, block, context)
//        val delay = VertxCoroutineExecutor(duplicatedContext).asCoroutineDispatcher() as Delay
//        return delay.invokeOnTimeout(timeMillis, block, context)
      }
    }
    val promise = Promise.promise<Any>()
    duplicatedContext.runOnContext {
      GlobalScope.launch(vertx.dispatcher()) {
        val neverEndingJob = launch {
          promise.future().await()
        }
        withContext(invokeTimeoutOnDuplicatedContextDispatcher) {
          // still on Vert.x
          testContext.assertEquals(duplicatedContext, ContextInternal.current())
          select<Unit> {
            neverEndingJob.onJoin {
            }
            onTimeout(100) {
              testContext.assertEquals(duplicatedContext, ContextInternal.current())
              neverEndingJob.cancel()
              latch.complete()
            }
          }
        }
      }
    }
  }

from vertx-lang-kotlin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.