{"action":"closed","issue":{"url":"https://api.github.com/repos/jordwest/imap-server/issues/19","labels_url":"https://api.github.com/repos/jordwest/imap-server/issues/19/labels{/name}","comments_url":"https://api.github.com/repos/jordwest/imap-server/issues/19/comments","events_url":"https://api.github.com/repos/jordwest/imap-server/issues/19/events","html_url":"https://github.com/jordwest/imap-server/issues/19","id":93145312,"number":19,"title":"Prevent data race between Listen() and Close()","user":{"login":"guns","id":55776,"avatar_url":"https://avatars.githubusercontent.com/u/55776?v=3","gravatar_id":"","url":"https://api.github.com/users/guns","html_url":"https://github.com/guns","followers_url":"https://api.github.com/users/guns/followers","following_url":"https://api.github.com/users/guns/following{/other_user}","gists_url":"https://api.github.com/users/guns/gists{/gist_id}","starred_url":"https://api.github.com/users/guns/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guns/subscriptions","organizations_url":"https://api.github.com/users/guns/orgs","repos_url":"https://api.github.com/users/guns/repos","events_url":"https://api.github.com/users/guns/events{/privacy}","received_events_url":"https://api.github.com/users/guns/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/jordwest/imap-server/labels/bug","name":"bug","color":"fc2929"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-07-05T20:35:57Z","updated_at":"2015-07-21T21:11:11Z","closed_at":"2015-07-21T21:11:11Z","body":"The Go Race detector complains about a data race between Listen() and Close(). To illustrate, apply the following patch to create an example test case:\r\n\r\n```patch\r\ndiff --git a/server_test.go b/server_test.go\r\nindex a242138..c50dd16 100644\r\n--- a/server_test.go\r\n+++ b/server_test.go\r\n@@ -6,6 +6,7 @@ import (\r\n        \"os\"\r\n        \"regexp\"\r\n        \"testing\"\r\n+       \"time\"\r\n\r\n        \"github.com/jordwest/imap-server/conn\"\r\n        \"github.com/jordwest/imap-server/mailstore\"\r\n@@ -288,3 +289,13 @@ func TestFetchFullMessageByUID(t *testing.T) {\r\n        r.expect(t, \" UID 11)\")\r\n        r.expect(t, \"abcd.123 OK UID FETCH Completed\")\r\n }\r\n+\r\n+func TestDataRace(t *testing.T) {\r\n+       s := NewServer(mailstore.NewDummyMailstore())\r\n+       s.Addr = \"127.0.0.1:10143\"\r\n+       go func() {\r\n+               s.ListenAndServe()\r\n+       }()\r\n+       time.Sleep(time.Millisecond)\r\n+       s.Close()\r\n+}\r\n\r\n```\r\n\r\nThen run `go test -race -run=DataRace`. The race detector should dump something like the following: (edited for readability)\r\n\r\n```\r\n==================\r\nWARNING: DATA RACE\r\nRead by goroutine 5:\r\n  github.com/jordwest/imap-server.(*Server).Close()\r\n      …/src/github.com/jordwest/imap-server/server.go:83 +0xcf\r\n  github.com/jordwest/imap-server.TestDataRace()\r\n      …/src/github.com/jordwest/imap-server/server_test.go:300 +0x2cd\r\n…\r\n\r\nPrevious write by goroutine 6:\r\n  github.com/jordwest/imap-server.(*Server).Listen()\r\n      …/src/github.com/jordwest/imap-server/server.go:55 +0x481\r\n  github.com/jordwest/imap-server.(*Server).ListenAndServe()\r\n      …/src/github.com/jordwest/imap-server/server.go:36 +0x47\r\n…\r\n==================\r\n==================\r\nWARNING: DATA RACE\r\nRead by goroutine 5:\r\n  net.(*TCPListener).Close()\r\n      …/go-1.4.2/src/net/tcpsock_posix.go:254 +0x4f\r\n  github.com/jordwest/imap-server.(*Server).Close()\r\n      …/src/github.com/jordwest/imap-server/server.go:86 +0x229\r\n…\r\n\r\nPrevious write by goroutine 6:\r\n  net.ListenTCP()\r\n      …/go-1.4.2/src/net/tcpsock_posix.go:298 +0x4aa\r\n  net.Listen()\r\n      …/go-1.4.2/src/net/dial.go:266 +0x559\r\n  github.com/jordwest/imap-server.(*Server).Listen()\r\n      …/src/github.com/jordwest/imap-server/server.go:50 +0x307\r\n  github.com/jordwest/imap-server.(*Server).ListenAndServe()\r\n      …/src/github.com/jordwest/imap-server/server.go:36 +0x47\r\n…\r\n==================\r\n```\r\n\r\nAs this project is young and there are many ways to address this (RWMutex, signaling over chans, etc), I am not submitting a pull request.\r\n\r\nThis is also not a critical race, since it happens on shutdown, but it does clutter the output of `go test -race`, which makes it harder to sort out application races.\r\n\r\nThanks again!\r\n"}}