# Ice Cream Makers and Data Races Part II

- Published: June 27, 2014
- Author: William Kennedy
- URL: https://www.ardanlabs.com/blog/2014/06/ice-cream-makers-and-data-races-part-ii.html

<span style="font-size: large;">Introduction</span><br />
Dave Cheney published a post called <a href="http://dave.cheney.net/2014/06/27/ice-cream-makers-and-data-races" target="_blank">Ice Cream Makers and Data Races</a>. The post showed an example of an interesting data race that can occur when using an interface typed variable to make a method call. If you have not read the post yet please do. Once you read the post you will discover that the problem lies with the fact that an interface value is implemented internally using a two word header and that the Go memory model states only writes to a single word are atomic.<br />
<br />
The program in the post shows a race condition that allows two goroutines to perform a read and write operation at the same time against an interface value. Not synchronizing this read and write allows the read to observe a partial write to the interface value. This allows the method implementation for the <span style="font-family: Courier New, Courier, monospace;">Ben</span> type to operate against a value of the <span style="font-family: Courier New, Courier, monospace;">Jerry</span> type and visa versa.<br />
<br />
In Dave's example, the layout of the <span style="font-family: Courier New, Courier, monospace;">Ben</span> and <span style="font-family: Courier New, Courier, monospace;">Jerry</span> structs were identical in memory, so they were in some sense compatible. Dave suggested the chaos that would occur if they had different memory representations. This is because each implementation of the <span style="font-family: Courier New, Courier, monospace;">Hello</span> method assumes the code is operating against values of the receiver type. When this bug surfaces, this is no longer the case. To give you a visual view of this suggested chaos, I am going to change the declaration of the <span style="font-family: Courier New, Courier, monospace;">Jerry</span> type in two different ways. Both changes will give you some better insight into the interworking of interface types and memory.<br />
<br />
<span style="font-size: large;">First Code Change</span><br />
Let's review the code and see the first set of changes. My changes to the original code are in bold:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
01 package main<br />
02<br />
03 import &quot;fmt&quot;<br />
04<br />
05 type IceCreamMaker interface {<br />
06 &nbsp; &nbsp;// Great a customer.<br />
07 &nbsp; &nbsp;Hello()<br />
08 }<br />
09<br />
10 type Ben struct {<br />
11 &nbsp; &nbsp;name string<br />
12 }<br />
13<br />
<b>14 func (b &#42;Ben) Hello() {</b><br />
<b>15 &nbsp; &nbsp;if b.name != &quot;Ben&quot; {</b><br />
<b>16 &nbsp; &nbsp; &nbsp; &nbsp;fmt.Printf(&quot;Ben says, \&quot;Hello my name is %s\&quot;\n&quot;, b.name)</b><br />
<b>17 &nbsp; &nbsp;}</b><br />
<b>18 }</b><br />
19<br />
<b>20 type Jerry struct {</b><br />
<b>21 &nbsp; &nbsp;field1 &#42;[5]byte</b><br />
<b>22 &nbsp; &nbsp;field2 int</b><br />
<b>23 }</b><br />
24<br />
<b>25 func (j &#42;Jerry) Hello() {</b><br />
<b>26 &nbsp; &nbsp;name := string((&#42;j.field1)[:])</b><br />
<b>27 &nbsp; &nbsp;if name != &quot;Jerry&quot; {</b><br />
<b>28 &nbsp; &nbsp; &nbsp; &nbsp;fmt.Printf(&quot;Jerry says, \&quot;Hello my name is %s\&quot;\n&quot;, name)</b><br />
<b>29 &nbsp; &nbsp;}</b><br />
<b>30 }</b><br />
31<br />
32 func main() {<br />
33 &nbsp; &nbsp;var ben = &amp;Ben{&quot;Ben&quot;}<br />
<b>34</b> &nbsp; &nbsp;<b>var jerry = &amp;Jerry{&amp;[5]byte{'J', 'e', 'r', 'r', 'y'}, 5}</b><br />
35 &nbsp; &nbsp;var maker IceCreamMaker = ben<br />
36<br />
37 &nbsp; &nbsp;var loop0, loop1 func()<br />
38<br />
39 &nbsp; &nbsp;loop0 = func() {<br />
40 &nbsp; &nbsp;maker = ben<br />
41 &nbsp; &nbsp;go loop1()<br />
42 &nbsp; &nbsp;}<br />
43<br />
44 &nbsp; &nbsp;loop1 = func() {<br />
45 &nbsp; &nbsp; &nbsp; &nbsp;maker = jerry<br />
46 &nbsp; &nbsp; &nbsp; &nbsp;go loop0()<br />
47 &nbsp; &nbsp;}<br />
48<br />
49 &nbsp; &nbsp;go loop0()<br />
50<br />
51 &nbsp; &nbsp;for {<br />
52 &nbsp; &nbsp; &nbsp; &nbsp;maker.Hello()<br />
53 &nbsp; &nbsp;}<br />
54 }
</div>
<br />
In the implementation of the <span style="font-family: Courier New, Courier, monospace;">Hello</span> method for the <span style="font-family: Courier New, Courier, monospace;">Ben</span> type on lines 14 through 18, I changed the code to only display the message when the name is not Ben. This is a simple change so we don't have to go through the results looking for when the bug surfaces.<br />
<br />
Then on lines 20 through 23, I completely changed out the declaration of the <span style="font-family: Courier New, Courier, monospace;">Jerry</span> type. The declaration is now a manual representation of a <span style="font-family: Courier New, Courier, monospace;">string</span>. A <span style="font-family: Courier New, Courier, monospace;">string</span> in Go consists of a header with two words. The first word is a pointer to an array of bytes and the second word is the length of the string. This is similar to a slice but without the third word in the header for the capacity. The declarations of the <span style="font-family: Courier New, Courier, monospace;">Ben</span> and <span style="font-family: Courier New, Courier, monospace;">Jerry</span> structs represent the same memory layout though declared very differently.<br />
<br />
The idea of changing the <span style="font-family: Courier New, Courier, monospace;">Jerry</span> type in this fashion is to show how serious this data race condition Dave created is. When the implementation of <span style="font-family: Courier New, Courier, monospace;">Hello</span>&nbsp;method for the <span style="font-family: Courier New, Courier, monospace;">Ben</span> type is called, the <span style="font-family: Courier New, Courier, monospace;">Printf</span> function is going to print the characters for the <span style="font-family: Courier New, Courier, monospace;">name</span> field. When the <span style="font-family: Courier New, Courier, monospace;">Hello</span> implementation for type <span style="font-family: Courier New, Courier, monospace;">Ben</span>&nbsp;is called using a value of type <span style="font-family: Courier New, Courier, monospace;">Jerry</span>, the memory associated with the <span style="font-family: Courier New, Courier, monospace;">name</span> field could be anything. In this case we are guaranteeing that there is a <span style="font-family: Courier New, Courier, monospace;">string</span>&nbsp;representation so the code does not stack trace, but doing this by unusual means.<br />
<br />
One lines 25 through 30, I changed the implementation of the <span style="font-family: Courier New, Courier, monospace;">Hello</span> method for the <span style="font-family: Courier New, Courier, monospace;">Jerry</span> type. I convert the <span style="font-family: Courier New, Courier, monospace;">byte</span> array to a <span style="font-family: Courier New, Courier, monospace;">string</span> and use the new <span style="font-family: Courier New, Courier, monospace;">name</span> variable to check and display the value. Since the declaration of the <span style="font-family: Courier New, Courier, monospace;">name</span> field in the <span style="font-family: Courier New, Courier, monospace;">Ben</span> type is equivalent to the declaration of the two fields declared in the <span style="font-family: Courier New, Courier, monospace;">Jerry</span> type, everything will print out appropriately.<br />
<br />
Finally on line 34, I create and initialize the variable of type Jerry, setting the bytes and length. Then the rest of the code remains as is.<br />
<br />
<span style="font-size: large;">Running The Program</span><br />
When we run this new version of the program the output does not change at all:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
Jerry says, &quot;Hello my name is Ben&quot;<br />
Ben says, &quot;Hello my name is Jerry&quot;<br />
Ben says, &quot;Hello my name is Jerry&quot;<br />
Jerry says, &quot;Hello my name is Ben&quot;<br />
Ben says, &quot;Hello my name is Jerry&quot;</div>
<br />
Even though the declaration of the <span style="font-family: Courier New, Courier, monospace;">Ben</span> and <span style="font-family: Courier New, Courier, monospace;">Jerry</span> types are different, the memory layouts are the same and the program runs as designed:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
type Ben struct {<br />
&nbsp; &nbsp;name string<br />
}<br />
<br />
type Jerry struct {<br />
&nbsp; &nbsp;field1 &#42;[5]byte<br />
&nbsp; &nbsp;field2 int<br />
}<br />
<br />
fmt.Printf(&quot;Ben says, \&quot;Hello my name is %s\&quot;\n&quot;, b.name)<br />
<br />
name := string((&#42;j.field1)[:])<br />
fmt.Printf(&quot;Jerry says, \&quot;Hello my name is %s\&quot;\n&quot;, name)</div>
<br />
In the case of the <span style="font-family: Courier New, Courier, monospace;">Printf</span> function call for the <span style="font-family: Courier New, Courier, monospace;">Ben</span> type implementation of the <span style="font-family: Courier New, Courier, monospace;">Hello</span> method, the code thinks the <span style="font-family: Courier New, Courier, monospace;">b</span> pointer is pointing to a value of type <span style="font-family: Courier New, Courier, monospace;">Ben</span> when it is not. However, since the memory layout is the same between the <span style="font-family: Courier New, Courier, monospace;">Ben</span> and <span style="font-family: Courier New, Courier, monospace;">Jerry</span> types, the call to the&nbsp;<span style="font-family: Courier New, Courier, monospace;">Printf</span>&nbsp;function still works.&nbsp;The same is true for the implementation of the <span style="font-family: Courier New, Courier, monospace;">Hello</span> method for the <span style="font-family: Courier New, Courier, monospace;">Jerry</span> type. The values of <span style="font-family: Courier New, Courier, monospace;">field1</span>&nbsp;and <span style="font-family: Courier New, Courier, monospace;">field2</span> are equivalent to declaring a <span style="font-family: Courier New, Courier, monospace;">string</span> field so everything works.<br />
<br />
<span style="font-size: large;">Crashing The Program</span><br />
Let's change the code one more time. This time we will make the <span style="font-family: Courier New, Courier, monospace;">Jerry</span> type incompatible with the <span style="font-family: Courier New, Courier, monospace;">Ben</span> type:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
01 package main<br />
02<br />
03 import &quot;fmt&quot;<br />
04<br />
05 type IceCreamMaker interface {<br />
06 &nbsp; &nbsp;// Great a customer.<br />
07 &nbsp; &nbsp;Hello()<br />
08 }<br />
09<br />
10 type Ben struct {<br />
11 &nbsp; &nbsp;name string<br />
12 }<br />
13<br />
14 func (b &#42;Ben) Hello() {<br />
15 &nbsp; &nbsp;if b.name != &quot;Ben&quot; {<br />
16 &nbsp; &nbsp; &nbsp; &nbsp;fmt.Printf(&quot;Ben says, \&quot;Hello my name is %s\&quot;\n&quot;, b.name)<br />
17 &nbsp; &nbsp;}<br />
18 }<br />
19<br />
<b>20 type Jerry struct {</b><br />
<b>21 &nbsp; &nbsp;field2 int</b><br />
<b>22 &nbsp; &nbsp;field1 &#42;[5]byte</b><br />
<b>23 }</b><br />
24<br />
25 func (j &#42;Jerry) Hello() {<br />
26 &nbsp; &nbsp;name := string((&#42;j.field1)[:])<br />
27 &nbsp; &nbsp;if name != &quot;Jerry&quot; {<br />
28 &nbsp; &nbsp; &nbsp; &nbsp;fmt.Printf(&quot;Jerry says, \&quot;Hello my name is %s\&quot;\n&quot;, name)<br />
29 &nbsp; &nbsp;}<br />
30 }<br />
31<br />
32 func main() {<br />
33 &nbsp; &nbsp;var ben = &amp;Ben{&quot;Ben&quot;}<br />
<b>34</b> &nbsp; &nbsp;<b>var jerry = &amp;Jerry{5,&nbsp;</b><b>&amp;[5]byte{'J', 'e', 'r', 'r', 'y'}</b><b>}</b><br />
35 &nbsp; &nbsp;var maker IceCreamMaker = ben<br />
36<br />
37 &nbsp; &nbsp;var loop0, loop1 func()<br />
38<br />
39 &nbsp; &nbsp;loop0 = func() {<br />
40 &nbsp; &nbsp;maker = ben<br />
41 &nbsp; &nbsp;go loop1()<br />
42 &nbsp; &nbsp;}<br />
43<br />
44 &nbsp; &nbsp;loop1 = func() {<br />
45 &nbsp; &nbsp; &nbsp; &nbsp;maker = jerry<br />
46 &nbsp; &nbsp; &nbsp; &nbsp;go loop0()<br />
47 &nbsp; &nbsp;}<br />
48<br />
<b>49 &nbsp; &nbsp;fmt.Printf(&quot;Ben: %p  Jerry: %p\n&quot;, ben, jerry)</b><br />
50<br />
51 &nbsp; &nbsp;go loop0()<br />
52<br />
53 &nbsp; &nbsp;for {<br />
54 &nbsp; &nbsp; &nbsp; &nbsp;maker.Hello()<br />
55 &nbsp; &nbsp;}<br />
56 }
</div>
<br />
Now the declaration of the <span style="font-family: Courier New, Courier, monospace;">Jerry</span> type between lines 20 through 23 switches the order of the two field members. The integer value now comes before the byte array pointer. When we run this version of the program we get a stack trace:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>Ben: 0x20817a170  Jerry: 0x20817a180</b><br />
<br />
01 panic: runtime error: invalid memory address or nil pointer dereference<br />
02 [signal 0xb code=0x1 addr=0x5 pc=0x294f6]<br />
03<br />
04 goroutine 16 [running]:<br />
05 runtime.panic(0xb90e0, 0x144144)<br />
06 &nbsp; &nbsp;/Users/bill/go/src/pkg/runtime/panic.c:279 +0xf5<br />
07 fmt.(&#42;fmt).padString(0x2081b42d0, 0x5, 0x20817a190)<br />
08 &nbsp; &nbsp;/Users/bill/go/src/pkg/fmt/format.go:130 +0x390<br />
09 fmt.(&#42;fmt).fmt_s(0x2081b42d0, 0x5, 0x20817a190)<br />
10 &nbsp; &nbsp;/Users/bill/go/src/pkg/fmt/format.go:285 +0x67<br />
11 fmt.(&#42;pp).fmtString(0x2081b4270, 0x5, 0x20817a190, 0x73)<br />
12 &nbsp; &nbsp;/Users/bill/go/src/pkg/fmt/print.go:511 +0xe0<br />
13 fmt.(&#42;pp).printArg(0x2081b4270, 0x97760, 0x20817a210, 0x73, 0x0, 0x0)<br />
14 &nbsp; &nbsp;/Users/bill/go/src/pkg/fmt/print.go:780 +0xbb8<br />
15 fmt.(&#42;pp).doPrintf(0x2081b4270, 0xddfd0, 0x20, 0x220832de40, 0x1, 0x1)<br />
16 &nbsp; &nbsp;/Users/bill/go/src/pkg/fmt/print.go:1159 +0x1ecc<br />
17 fmt.Fprintf(0x220818c340, 0x2081c2008, 0xddfd0, 0x20, 0x220832de40, 0x1, 0x1, 0x10, 0x0, 0x0)<br />
18 &nbsp; &nbsp; /Users/bill/go/src/pkg/fmt/print.go:188 +0x7f<br />
19 fmt.Printf(0xddfd0, 0x20, 0x220832de40, 0x1, 0x1, 0x5, 0x0, 0x0)<br />
20 &nbsp; &nbsp;/Users/bill/go/src/pkg/fmt/print.go:197 +0xa2<br />
<b>21 main.(&#42;Ben).Hello(0x20817a180)</b><br />
22 &nbsp; &nbsp;/Users/bill/Spaces/Go/Projects/src/github.com/goinaction/code/temp/main.go:16 +0x118<br />
23 main.main()<br />
24 &nbsp; &nbsp;/Users/bill/Spaces/Go/Projects/src/github.com/goinaction/code/temp/main.go:54 +0x2c3</div>
<br />
If we look at line 21 of the stack trace we will see how the method call to <span style="font-family: Courier New, Courier, monospace;">Hello</span> is using the implementation for type <span style="font-family: Courier New, Courier, monospace;">Ben</span> but being passed the address of the value for type <span style="font-family: Courier New, Courier, monospace;">Jerry</span>. Just before the stack trace I display the addresses of each value to make this clear. If we look at the declaration of the <span style="font-family: Courier New, Courier, monospace;">Ben</span> and <span style="font-family: Courier New, Courier, monospace;">Jerry</span> types one more time we can see how they are no longer compatible:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
type Ben struct {<br />
&nbsp; &nbsp;name string<br />
}<br />
<br />
type Jerry struct {<br />
&nbsp; &nbsp;field2 int<br />
&nbsp; &nbsp;field1 &#42;[5]byte<br />
}</div>
<br />
Since this new declaration for type <span style="font-family: Courier New, Courier, monospace;">Jerry</span>&nbsp;now starts with an integer value, it is not compatible with a <span style="font-family: Courier New, Courier, monospace;">string</span> type. This time when the code tries to print the value of <span style="font-family: Courier New, Courier, monospace;">b.name</span> on line 16, the program stack traces.<br />
<br />
<span style="font-size: large;">Conclusion</span><br />
In the end, a running program manipulates memory without any safeguards from the compiler. Memory is just memory and the CPU will interpret that memory as it is told. In the crash example, thanks to the data race bug, the code asked the CPU to interpret an integer value as a string and the program crashed. So I agree with Dave, there is no such thing as a safe data race.&nbsp;Your program either has no data races, or its operation is undefined.<br />
<br />
<span style="font-size: large;">Cherry On Top</span><br />
After reading both posts you might be disappointed that no ice cream was actually used in the writing of these posts. If you're bummed out now and wished you had some coupons, I found a link to signup for the Ben and Jerry's <a href="http://www.benjerry.com/about-us/contact-us?selectedForm=newsletter#selectForm" target="_blank">newsletter</a>. You can also <a href="http://www.benjerry.com/" target="_blank">find a store</a> near you.
