Open DarkHeros09 opened 1 month ago
(Hey! I'm not the maintainer, just a user who saw your issue:)
This library neither uses Goroutines nor does any synchronization for you. The interface is therefore not thread-safe, which is why your application correctly trips the race detector.
If you call .Set()
on a token from multiple Goroutines, then you need to explicitly synchronize access to it, just like with any other datastructure in Go. This is not an issue in this library. Using sync.Mutex
is the right way to do that. Another way would be to structure your application so that only a single Goroutine constructs the token.
(Hey! I'm not the maintainer, just a user who saw your issue:)
This library neither uses Goroutines nor does any synchronization for you. The interface is therefore not thread-safe, which is why your application correctly trips the race detector.
If you call
.Set()
on a token from multiple Goroutines, then you need to explicitly synchronize access to it, just like with any other datastructure in Go. This is not an issue in this library. Usingsync.Mutex
is the right way to do that. Another way would be to structure your application so that only a single Goroutine constructs the token.
Hi
Thank you for your reply.
I already used sync.Mutex
and i still get the same error.
I reverted back to o1egl/paseto
package since it didn't have this problem.
I hope this issue get solved.
Please post a reproducer code snippet. We're a heavy user of this lib too so I'm interested in it having no critical bugs, would investigate it based on your code. The tests of the lib pass fine under the Go race checker in the environments I have here (Ubuntu 24.04 amd64, macOS 14.7 arm64).
I'll second the request for a repro snippet, ideally showing how you're using sync.Mutex
to set properties on a token.
This library (like Go's own data structures such as map[_]_
) isn't designed to handle data races transparently. Instead if you're using multiple goroutines to write to a single token, it would be up to you to impose locks to ensure concurrent writes don't happen.
Outside of race safety with the underlying data structures, concurrent writes may also cause issues (e.g. if two processes write to the same key, the order of writes would change the end result). For this reason, I'm not sure it's possible for this library to make general promises about race safety when building a token (since there are logic problems that can occur, rather than just data structure related ones).
My codebase is quite large, so I'll work on producing a minimal code snippet when I get the chance. It might take a little while, as I'm tied up with other tasks at the moment, but I'll get back to you when I can.
Hi
thank you for making this package. I've been using it for a month or so now, and i noticed that i get data race error from time to time
i solved it by using
sync.mutex
, but i don't know if that's the right solution or whether will it affect my backend performance.i don't know if implementing
syncmap.Map{}
will fix this issue.