Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4657)

Issue 180068: runtime: fix race condition (Closed)

Can't Edit
Can't Publish+Mail
Start Review
Created:
14 years, 4 months ago by agl1
Modified:
14 years, 4 months ago
Reviewers:
CC:
rsc, golang-dev
Visibility:
Public.

Description

runtime: fix race condition (Thanks to ken and rsc for pointing this out) rsc: ken pointed out that there's a race in the new one-lock-per-channel code. the issue is that if one goroutine has gone to sleep doing select { case <-c1: case <-c2: } and then two more goroutines try to send on c1 and c2 simultaneously, the way that the code makes sure only one wins is the selgen field manipulation in dequeue: // if sgp is stale, ignore it if(sgp->selgen != sgp->g->selgen) { //prints("INVALID PSEUDOG POINTER\n"); freesg(c, sgp); goto loop; } // invalidate any others sgp->g->selgen++; but because the global lock is gone both goroutines will be fiddling with sgp->g->selgen at the same time. This results in a 7% slowdown in the single threaded case for a ping-pong microbenchmark. Since the cas predominantly succeeds, adding a simple check first didn't make any difference.

Patch Set 1 #

Patch Set 2 : code review 180068: runtime: fix race condition #

Patch Set 3 : code review 180068: runtime: fix race condition #

Patch Set 4 : code review 180068: runtime: fix race condition #

Unified diffs Side-by-side diffs Delta from patch set Stats (+89 lines, -5 lines) Patch
M src/pkg/runtime/chan.c View 1 2 2 chunks +2 lines, -4 lines 0 comments Download
M src/pkg/runtime/runtime.h View 1 chunk +1 line, -1 line 0 comments Download
A test/chan/doubleselect.go View 1 chunk +83 lines, -0 lines 0 comments Download
M test/golden.out View 1 chunk +3 lines, -0 lines 0 comments Download

Messages

Total messages: 6
agl1
14 years, 4 months ago (2009-12-16 21:00:59 UTC) #1
rsc
LGTM but please make selgen a uint32 so that you don't need the cast. On ...
14 years, 4 months ago (2009-12-16 21:28:19 UTC) #2
ken3
i am not sure that this fixes the race. please come see me. On Wednesday, ...
14 years, 4 months ago (2009-12-16 21:32:57 UTC) #3
agl1
PTAL
14 years, 4 months ago (2009-12-16 23:01:58 UTC) #4
rsc
LGTM
14 years, 4 months ago (2009-12-18 07:03:23 UTC) #5
agl1
14 years, 4 months ago (2009-12-18 20:25:57 UTC) #6
*** Submitted as http://code.google.com/p/go/source/detail?r=2ccb1970a8a5 ***

runtime: fix race condition

(Thanks to ken and rsc for pointing this out)

rsc:
	ken pointed out that there's a race in the new
	one-lock-per-channel code.  the issue is that
	if one goroutine has gone to sleep doing

	select {
	case <-c1:
	case <-c2:
	}

	and then two more goroutines try to send
	on c1 and c2 simultaneously, the way that
	the code makes sure only one wins is the
	selgen field manipulation in dequeue:

	       // if sgp is stale, ignore it
	       if(sgp->selgen != sgp->g->selgen) {
		       //prints("INVALID PSEUDOG POINTER\n");
		       freesg(c, sgp);
		       goto loop;
	       }

	       // invalidate any others
	       sgp->g->selgen++;

	but because the global lock is gone both
	goroutines will be fiddling with sgp->g->selgen
	at the same time.

This results in a 7% slowdown in the single threaded case for a
ping-pong microbenchmark.

Since the cas predominantly succeeds, adding a simple check first
didn't make any difference.

R=rsc
CC=golang-dev
http://codereview.appspot.com/180068
Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b