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

data race in state tracking #49

Closed
StalkR opened this issue Nov 17, 2014 · 13 comments
Closed

data race in state tracking #49

StalkR opened this issue Nov 17, 2014 · 13 comments

Comments

@StalkR
Copy link
Contributor

StalkR commented Nov 17, 2014

Nick isn't safe for concurrent access https://github.com/fluffle/goirc/blob/master/state%2Fnick.go#L15

An example of this issue was reported in StalkR/goircbot#8 where there is a goroutine that checks bot Channels() (a read on Nick), meanwhile the state handling using addChannel (a write on Nick).

I was thinking we could add a mutex in that struct, then protect access to the members (lock/defer unlock).
Thoughts?

@fluffle
Copy link
Owner

fluffle commented Nov 17, 2014

State tracking is potentially racey -- see the other open issue. In current master, state tracker handlers are run strictly before any other handlers for a particular event, and the (default) foreground handlers are run in parallel for that same event while blocking the runloop goroutine. Are you using the "background" handlers? Otherwise the state handlers ought to be completely done with their writes by the time your reads can execute. Or, i've messed something up, which is entirely plausible. I've not actually used the state tracking in anger ever, it was just ... practice.

@StalkR
Copy link
Contributor Author

StalkR commented Nov 17, 2014

It doesn't use any handler, it's a goroutine which shares the *client.Conn, calls Me() to get *state.Nick then Channels() to get the list of channels.

@fluffle
Copy link
Owner

fluffle commented Dec 20, 2014

The idea of scattering locks all throughout this code is making me feel a bit ill, despite all the races inherent in the current design. Some handwaving:

The API presented by the current state tracker is a little ... public, I think. I don't know that external users of the package need the ability to create arbitrary nicks or channels except via the tracker. I was considering making all the tracker methods return struct copies of Nicks and Channels rather than pointers to the originals. More of the actual tracking could be moved into the tracker where fewer, coarser locks would be required, and the copies would represent the state of the tracker at the point the method was called. Mutating them would not affect internal tracker state.

Just a vague idea. Thoughts? I might have some time over Christmas to hack on code ;-)

@StalkR
Copy link
Contributor Author

StalkR commented Dec 20, 2014

+1 on returning copies. You'll still need locks but you can have one RWMutex per struct and the usual lock/defer unlock on methods.

@fluffle
Copy link
Owner

fluffle commented Jan 1, 2015

@lucy @StalkR @stzanpet PTAL at the diff between master and state-copy. There's more to come -- I'm working on the changes to client/. Or, I would be, but a friend bought me KSP for Christmas :-D

@fluffle
Copy link
Owner

fluffle commented Jan 2, 2015

Client changes pushed and tested. Things seem fine. This will probably break code that uses the state tracker, but that's probably also a good thing.

@sztanpet (spelled it wrong last time, sorry)

@sztanpet
Copy link

sztanpet commented Jan 2, 2015

cheers for trying to take everybodies viewpoint into account, and happy new year!
(my code continues to work fine btw, also including me as a contributor is far-fetched but appreciated)

@StalkR
Copy link
Contributor Author

StalkR commented Jan 27, 2015

Cool @fluffle! As expected goircbot breaks, I'll start a branch with the changes like you did and test, then we can merge to our master when ready.

@StalkR
Copy link
Contributor Author

StalkR commented Jan 28, 2015

Done and it seems to works fine. Ready to merge in master when you are!

@StalkR
Copy link
Contributor Author

StalkR commented Feb 12, 2015

ping @fluffle :)

@fluffle
Copy link
Owner

fluffle commented Feb 12, 2015

Sorry, bit busy with life. And, well Minecraft ;-)
On 12 Feb 2015 19:40, "StalkR" notifications@github.com wrote:

ping @fluffle https://github.com/fluffle :)


Reply to this email directly or view it on GitHub
#49 (comment).

@TrollWarlord
Copy link

You replied but didn't accept the merge?

@fluffle
Copy link
Owner

fluffle commented Feb 27, 2015

Accept what merge? I've not merged my "state-copy" branch into my master yet because I've been busy, as I said. Fortunately for you I've got some time spare this evening ;-)

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

No branches or pull requests

4 participants