Vert.x timer thread safety

284 views
Skip to first unread message

Tomasz Gac

unread,
Jul 22, 2022, 9:24:03 AM7/22/22
to vert.x
Hi,

We've been using Vert.x for many years with great success, but there is a rumor circulating in our company regarding Vertx.setTimer method. Supposedly, it has no guarantees regarding thread safety - the timer will be run on some thread, instead of the context of the verticle it was called on.

We are setting up our vertices in the AbstractVerticle.start() method by registering event bus consumers and creating various timers. Is there a possibility of race between these handlers?

I ran some tests, and I was unable to detect race conditions when I run them in a production environment.

Julien Ponge

unread,
Jul 22, 2022, 9:45:06 AM7/22/22
to ve...@googlegroups.com
The rumor is likely hiding some weird usage of Vert.x, because multiple setTimer on a given context will have the callbacks be run on the same context :-)

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/9af6d7ab-9884-4a61-975f-90f92b74b0d1n%40googlegroups.com.

Julien Viet

unread,
Aug 1, 2022, 7:39:55 AM8/1/22
to vert.x
Hi,

if you are creating the timer from

- a vertx event-loop thread, the timer will always be executed by the
same thread
- a vertx worker thread, the timer will always be executed from the
same context but not the same worker thread. However the context will
execute one task at a time and issue a memory barrier so that there is
no race happening
- from a non vertx thread
- in vertx 3, you get a random event-loop each time you create the timer
- in vertx 4, you get a random event-loop assigned the first you the
timer is created (or before if a context was obtained before from the
non vertx thread)

HTH

Julien
Reply all
Reply to author
Forward
0 new messages