Ice Cream Makers and Data Races Part II

Ice Cream Makers and Data Races Part II

Introduction Dave Cheney published a post called Ice Cream Makers and Data Races. 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.

Introduction

Dave Cheney published a post called Ice Cream Makers and Data Races. 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.

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 Ben type to operate against a value of the Jerry type and visa versa.

In Dave’s example, the layout of the Ben and Jerry 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 Hello 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 Jerry type in two different ways. Both changes will give you some better insight into the interworking of interface types and memory.

First Code Change

Let’s review the code and see the first set of changes. My changes to the original code are in bold:

01 package main

02

03 import "fmt"

04

05 type IceCreamMaker interface {

06  // Great a customer.

07  Hello()

08 }

09

10 type Ben struct {

11  name string

12 }

13

14 func (b *Ben) Hello() {

15  if b.name != "Ben" {

16    fmt.Printf("Ben says, "Hello my name is %s"\n", b.name)

17  }

18 }

19

20 type Jerry struct {

21  field1 *[5]byte

22  field2 int

23 }

24

25 func (j *Jerry) Hello() {

26  name := string((*j.field1)[:])

27  if name != "Jerry" {

28    fmt.Printf("Jerry says, "Hello my name is %s"\n", name)

29  }

30 }

31

32 func main() {

33  var ben = &Ben{"Ben"}

34   var jerry = &Jerry{&[5]byte{‘J’, ‘e’, ‘r’, ‘r’, ‘y’}, 5}

35  var maker IceCreamMaker = ben

36

37  var loop0, loop1 func()

38

39  loop0 = func() {

40  maker = ben

41  go loop1()

42  }

43

44  loop1 = func() {

45    maker = jerry

46    go loop0()

47  }

48

49  go loop0()

50

51  for {

52    maker.Hello()

53  }

54 }

In the implementation of the Hello method for the Ben 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.

Then on lines 20 through 23, I completely changed out the declaration of the Jerry type. The declaration is now a manual representation of a string. A string 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 Ben and Jerry structs represent the same memory layout though declared very differently.

The idea of changing the Jerry type in this fashion is to show how serious this data race condition Dave created is. When the implementation of Hello method for the Ben type is called, the Printf function is going to print the characters for the name field. When the Hello implementation for type Ben is called using a value of type Jerry, the memory associated with the name field could be anything. In this case we are guaranteeing that there is a string representation so the code does not stack trace, but doing this by unusual means.

One lines 25 through 30, I changed the implementation of the Hello method for the Jerry type. I convert the byte array to a string and use the new name variable to check and display the value. Since the declaration of the name field in the Ben type is equivalent to the declaration of the two fields declared in the Jerry type, everything will print out appropriately.

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.

data science

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

What Are The Advantages and Disadvantages of Data Science?

Online Data Science Training in Noida at CETPA, best institute in India for Data Science Online Course and Certification. Call now at 9911417779 to avail 50% discount.

50 Data Science Jobs That Opened Just Last Week

Data Science and Analytics market evolves to adapt to the constantly changing economic and business environments. Our latest survey report suggests that as the overall Data Science and Analytics market evolves to adapt to the constantly changing economic and business environments, data scientists and AI practitioners should be aware of the skills and tools that the broader community is working on. A good grip in these skills will further help data science enthusiasts to get the best jobs that various industries in their data science functions are offering.

Data Science With Python Training | Python Data Science Course | Intellipaat

🔵 Intellipaat Data Science with Python course: https://intellipaat.com/python-for-data-science-training/In this Data Science With Python Training video, you...

Applications Of Data Science On 3D Imagery Data

The agenda of the talk included an introduction to 3D data, its applications and case studies, 3D data alignment and more.

Data Science Course in Dallas

Become a data analysis expert using the R programming language in this [data science](https://360digitmg.com/usa/data-science-using-python-and-r-programming-in-dallas "data science") certification training in Dallas, TX. You will master data...