Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED] Deadlock when accessing subscriptions map on consumer #1671

Merged
merged 2 commits into from
Jul 25, 2024

Conversation

piotrpio
Copy link
Collaborator

This fixes an issue where a deadlock could occur when calling Stop() or
Drain() on ConsumeContext or MessagesContext and then calling Consume or
Messages immediately.
Switched to using a type-safe implementation of sync.Map for subscriptions map
instead of locking the whole consumer state.
Additionally, changed the type of atomic flags from uint32 to atomic.UInt32
to avoid acciental non-atomic reads/writes.

Signed-off-by: Piotr Piotrowski piotr@synadia.com

@piotrpio piotrpio requested a review from Jarema July 10, 2024 16:32
This fixes an issue where a deadlock could occur when calling `Stop()` or
`Drain()` on `ConsumeContext` or `MessagesContext` and then calling `Consume` or
`Messages` immediately.
Switched to using a type-safe implementation of `sync.Map` for subscriptions map
instead of locking the whole consumer state.
Additionally, changed the type of atomic flags from `uint32` to `atomic.UInt32`
to avoid acciental non-atomic reads/writes.

Signed-off-by: Piotr Piotrowski <piotr@synadia.com>
Copy link
Member

@Jarema Jarema left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
Just one comment on the type safe sync map.

return previous.(V), loaded
}

func (m *Map[K, V]) Len() int {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is not concurrent safe.

Maybe add additional field, like

count atomic.Int64

and increment/decrement it every time we change the map?
That would also make Len() more performant, I think for a very low price elsewhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a counter would not work since e.g. when using Store() we don't know whether the value is added to the map or updated. I agree that it may be racy, I'll think of another solution to not have to use Len() at all.

Signed-off-by: Piotr Piotrowski <piotr@synadia.com>
Copy link
Member

@Jarema Jarema left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@piotrpio piotrpio merged commit c693ec3 into main Jul 25, 2024
4 checks passed
@piotrpio piotrpio deleted the js-deadlock branch July 25, 2024 08:29
@piotrpio piotrpio mentioned this pull request Aug 13, 2024
piotrpio added a commit that referenced this pull request Aug 15, 2024
This fixes an issue where a deadlock could occur when calling `Stop()` or
`Drain()` on `ConsumeContext` or `MessagesContext` and then calling `Consume` or
`Messages` immediately.
Switched to using a type-safe implementation of `sync.Map` for subscriptions map
instead of locking the whole consumer state.
Additionally, changed the type of atomic flags from `uint32` to `atomic.UInt32`
to avoid accidental non-atomic reads/writes.

Signed-off-by: Piotr Piotrowski <piotr@synadia.com>

---------

Signed-off-by: Piotr Piotrowski <piotr@synadia.com>
piotrpio added a commit that referenced this pull request Aug 15, 2024
This fixes an issue where a deadlock could occur when calling `Stop()` or
`Drain()` on `ConsumeContext` or `MessagesContext` and then calling `Consume` or
`Messages` immediately.
Switched to using a type-safe implementation of `sync.Map` for subscriptions map
instead of locking the whole consumer state.
Additionally, changed the type of atomic flags from `uint32` to `atomic.UInt32`
to avoid accidental non-atomic reads/writes.

Signed-off-by: Piotr Piotrowski <piotr@synadia.com>

---------

Signed-off-by: Piotr Piotrowski <piotr@synadia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants