What happens it I update Item state in a rule, triggered by this item?

I wonder what happens if I have an item, which triggers some rule, based on it’s state change.
And then in this rule I want to assign new state to this item (so called state machine) (via postUpdate function).
What happens next? Will the rule be triggered immediately after this assignment? Or not triggered at all?
Will there be any re-entrant problems?

The rule will be retriggered immediatly.

Almost immediately and it will occur in a separate thread. Note: by “assign new state” I assume you mean calling sendCommand or postUpdate. Simply trying to do something like MyItem.state = ON will do nothing and upon exiting the rule MyItem will not retain its new state.

Potentially as the the new rule will be running in a separate thread so depending on timing and what else your rule does it is possible that the original rule will still be running when the new rule starts. So if you modify state at the top or modify the state at the bottom of your rule you could hit some problems.

Hmm, so that is not the behaiviour I would expect. E.g. It would be nice if the rule would be retriggered immediately, but after the original rule ends. Otherwise it’s becoming complex to manage…

Welcome to multi-threaded programming. That isn’t how even-bus based architectures work. Each message (i.e. state change to an Item) gets processed independently.

However, you can use a reentrant lock on your rule so only one thread can be in the rule at a time. That will do what you want.

But it is really not a problem if the last thing or nearly the last thing you do in the rule, or there is nothing that changes state anywhere occurs after you post the new state. It takes a hundred msecs or so for the new state to go out to the event bus and come back to trigger the rule. It also isn’t a problem if the only state being changed in the rule (or other rules that get triggered by this rule) beyond your state-machine Item.

And, in my experience, if you are needing to resort to all sorts of things in the language which seem awkward, there is almost always a better way to go about doing it.