Also, this is consistent with your observation that calling peer.Key again
returns a valid string. After the first call (returning the invalid string)
the concurrent modification of p.str64 has finished and subsequent calls
return the full value.

Which is to say: Run your code under the race detector. It should catch
this.

You mentioned above that you checked that all accesses to "unsafe
structures like maps or arrays" are protected by mutexes. But that's a
fallacy. *Any* concurrent access must be made safe. There is no such thing
as a benign data race.

On Mon, Sep 26, 2022 at 11:20 AM Axel Wagner <axel.wagner...@googlemail.com>
wrote:

> On Mon, Sep 26, 2022 at 9:03 AM Bernd Fix <b...@hoi-polloi.org> wrote:
>
>> Sure:
>>
>> // Key returns a string used for map operations
>> func (p *PeerID) Key() string {
>>      if p == nil {
>>          return "(nil)"
>>      }
>>      if len(p.str64) == 0 {
>>          p.str64 = base64.StdEncoding.EncodeToString(p.Data)
>>      }
>>      return p.str64
>> }
>>
>
> Looking at this code, the only cay I can imagine this returning such an
> invalid string is a data race.
> Note that calling PeerID.Key is not concurrency safe, as it both reads an
> writes p.str64, without any sort of mutex or atomicity - but it might very
> well *seem* that way, as it might *seem* it should be a read-only operation.
> So I would guess something is calling this method concurrently (or calling
> it concurrently with a similar method), causing a data-race, returning a
> string which has the length of the base64.EncodeToString return value, but
> not the data pointer.
>
>
>>
>> > Is it (or any function it calls) using "unsafe"?
>>
>> As said before, "sdlcanvas" as a cgo library might use that but it is
>> not logically involved in the process that panics.    >Y<
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golang-nuts+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/golang-nuts/3a7e6721-1a4e-c789-97aa-132f1a76466b%40hoi-polloi.org
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfH5GkBevWt4LJ1YHJMpPXemRue_W2Xa%3DONQsAcvF%2BE%3DuA%40mail.gmail.com.

Reply via email to