substrate icon indicating copy to clipboard operation
substrate copied to clipboard

Make unbounded channels size warning exact (part 1)

Open dmitry-markin opened this issue 3 years ago • 0 comments

This is a follow-up to https://github.com/paritytech/substrate/pull/12971 & https://github.com/paritytech/substrate/pull/13020. As discussed in https://github.com/paritytech/substrate/pull/12971#discussion_r1063641102, it's desirable to set exact warning threshold on the unbounded channel queue size.

The implementation relies on Release & Acquire memory ordering properties to make sure the counter modification order is increment first and then decrement (and therefore we don't hit the unsigned integer underflow). This relies on the following guarantees:

A load operation with this memory order performs the acquire operation on the affected memory location: no reads or writes in the current thread can be reordered before this load.

and

A store operation with this memory order performs the release operation: no reads or writes in the current thread can be reordered after this store.

(See https://en.cppreference.com/w/cpp/atomic/memory_order)

Nevertheless, I'm not 100% confident that the causality relationship between unbounded_send() and poll_next() (see code comments) really works as expected in this context. So any review with proper understanding of memory ordering implications is greatly appreciated.

If concurrency-wise this PR is OK, I will extend the same exact warning semantics on mpsc::tracing_unbounded() in a follow-up PR.

CC @nazar-pc

dmitry-markin avatar Jan 10 '23 14:01 dmitry-markin