<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Ben Muthalaly</title>
  <link href="https://benmuth.github.io/atom.xml" rel="self"/>
  <link href="https://benmuth.github.io"/>
  <updated>2023-06-14T11:55:00-05:00</updated>
  <id>https://benmuth.github.io/</id>
  <author>
    <name>Ben Muthalaly</name>
    <email>benmuthalaly@gmail.com</email>
  </author>

  <entry>
<title>Monitor Goroutines</title>
<link href="https://benmuth.github.io/posts/monitor-goroutines.html"/>
<id>https://benmuth.github.io/posts/monitor-goroutines.html</id>
<updated>2023-06-14T11:55:00-05:00</updated>
<summary>A simple way to work with shared memory in Go</summary>
<content type="html"><![CDATA[<p>This is about a small refactoring I did to fix some data races using basic Go concurrency primitives. This might be interesting to you if you’re familiar with Go’s concurrency model but haven’t played with it much.</p>
<p>Recently, I had to deal with data races for the first time while working on a <a href="https://github.com/burntcarrot/pairpad">collaborative terminal text editor</a>. After adding a new feature, I ran the program through Go’s handy dandy <a href="https://go.dev/doc/articles/race_detector">data race detector</a>, and unfortunately, it was not very happy with me. I set about fixing the detected races. Halfway through some refactoring, I came upon the idea of <a href="https://en.wikipedia.org/wiki/Monitor_(synchronization)">monitor</a> goroutines in <a href="https://gopl.io">The Go Programming Language</a> as a way to protect shared memory. This idea was already pretty similar to our original design, and the natural implementation kind of fell out of the concurrency primitives in Go, so I found it a helpful way to think about solving conflicting memory access.</p>
<p>Here’s a rough sketch of what our code used to look like:</p>
<pre><code class="language-go">// Editor holds all the state of an editing session.
type Editor struct {
	Text [][]string
	StatusMsg string
	// other stuff...
}

// listenForInput listens for input events and sends them over the
// returned channel
func listenForInput() chan InputEvent {
	inputChan := make(chan InputEvent)
	go func() {
		for {
			inputChan &lt;- PollEvent()
		}
	}()
	return inputChan
}

// listenForMessages listens over the connection for JSON messages
// and sends them over the returned  channel.
func listenForMessages() chan Message {
	messageChan := make(chan Message)
	go func() {
		for {
			// get some JSON message from connection
			messageChan &lt;- someJSONMessage
		}
	}
	return messageChan
}

// mainLoop listens on the local and remote message channels to
// update the editor contents.
func (e *Editor) mainLoop() {
	inputChan := listenForInput()
	messageChan := listenForMessages()
	
	for {
		select {
		case input := &lt;- inputChan:
			e.LocalUpdate(input)
		case message := &lt;- messageChan:
			e.RemoteUpdate(message)	
		}
	}
}

// LocalUpdate updates editor contents with the contents of the user
// input event using the CRDT insertion/deletion, and renders the result.
func (e *Editor) LocalUpdate(input InputEvent) {
	// ...update editor state
	e.Draw()
}

// RemoteUpdate updates the editor contents with the contents of the
// remote message using the CRDT insertion/deletion, and renders the result.
func (e *Editor) RemoteUpdate(message Message) {
	// ...update editor state
	e.Draw()
}

// Draw renders the editor contents to the terminal.
func (e *Editor) Draw() {
	for lineNumber, line := range e.Text {
		// ...render text buffer to terminal
	}

	// ...render e.StatusMsg to last line in terminal
}
</code></pre>
<p>Our actual implementation wasn’t exactly like this, and I’ve left out all the details, but this should be sufficient for now.</p>
<p>From what I’ve shown here, this design seems fine, because all editor access happens in the <code>for</code> loop in the <code>mainLoop</code> function. <code>LocalUpdate</code> and <code>RemoteUpdate</code> don’t spawn their own goroutines, so that <code>select</code> statement actually blocks the goroutines while each case is executing its update and render. It’s not an optimal use of computer resources, but it’s a simple way to get concurrency safety.</p>
<p>The problem with our design was how we implemented updating the status message. The status messag was (in part) for alerting the user to errors like failing to save to a file, or a message indicating you had disconnected from the server, and would be displayed on the last line of the terminal window.</p>
<p>In order to get timely updates on exactly when something went wrong, we were updating the status message (<code>Editor.StatusMsg</code>)  and calling <code>Editor.Draw()</code> the moment the error was detected. Errors could happen at any time in multiple active goroutines. Instead of having a pipeline sending messages from each place there could be an error, we were updating and drawing the status message all over the place. It was messy and full of trivial data races.</p>
<p>If you’re faced with data races and don’t know exactly what to do, consider the advice at the top of <a href="https://go.dev/ref/mem">the article on Go’s memory model</a>: “Programs that modify data being simultaneously accessed by multiple goroutines must serialize such access. To serialize access, protect the data with channel operations”.</p>
<p>They make it sound so simple! And while data races can be <a href="%5Buber%5D(https://www.uber.com/en-MX/blog/data-race-patterns-in-go/)">very tricky</a>, in our case it does seem to be relatively simple.</p>
<p>Let’s start from the inside-out. <code>Editor.Draw()</code> accesses a bunch of state at once, like the text buffer and the status message. How do we serialize it? Let’s try using a channel:</p>
<pre><code class="language-go">func drawLoop() {
	for &lt;- e.DrawChan {
		e.Draw()
	}
}

func (e *Editor) SendDraw() {
	e.DrawChan &lt;- 1
}
</code></pre>
<p>So what does this mean? We’re using a channel to serialize access to the draw function. So if we replace all of the instances of calling <code>Editor.Draw()</code> in-line with <code>Editor.SendDraw()</code>, the channel receive operation in the <code>drawLoop</code> (the <code>&lt;- e.DrawChan</code> part) will block any concurrent calls to <code>Editor.SendDraw()</code> until the current <code>e.Draw()</code> call finishes. Just like that, <code>e.Draw()</code> is serialized.</p>
<p>Note that this is reminiscent of our previous design:</p>
<pre><code class="language-go">func (e *Editor) mainLoop() {
	inputChan := listenForInput()
	messageChan := listenForMessages()
	
	for {
		select {
		case input := &lt;- inputChan:
			e.LocalUpdate(input)
		case message := &lt;- messageChan:
			e.RemoteUpdate(message)	
		}
	}
}
</code></pre>
<p><code>Editor.LocalUpdate</code> and <code>Editor.RemoteUpdate</code> both contain a call to <code>Draw</code>, but there won’t be a data race between them because they’re in the same goroutine. The trouble with the design comes when you try to scale to contain more goroutines with more calls to <code>Draw</code> (like for updating the status bar). With the old design, to safely deal with more goroutines, you have to set up a separate pipeline of messages, and add a channel to the select statement, even if all you’re doing is a read of the text buffer (like when you’re drawing the text to the terminal). With the new design, we don’t have to worry so much about the plumbing, because <code>e.Draw</code> is hidden behind the <code>drawChan</code> that will act as our “monitor”. If everyone shares the same pipeline, then everyone has to wait in line to get to what’s at the end of the pipe. No racing allowed!</p>
<p>Now, to finish up, we just have to call <code>go drawLoop()</code> sometime before our main loop, and it will be there waiting patiently any time we have to update the terminal.</p>
<p>There’s still the question of how exactly to fix status updates, since the status bar isn’t included in this pipeline. The solution I went with uses another monitor. Now, every time we want to update the status message, which can happen from any goroutine, we send the new status message to the <code>StatusChan</code>, ensuring status updates are serialized. We have another goroutine running a <code>for</code> loop that listens for messages on the <code>StatusChan</code>, updates the status message behind a lock, and sends draw requests with <code>Editor.SendDraw()</code>, where access to the status message is also protected by a lock. The monitor (<code>StatusChan</code>) isn’t strictly necessary here, because I’m still using a lock to allow for concurrent access of the <code>StatusMsg</code> by <code>Editor.Draw</code>. I could have just called <code>updateStatusMessage()</code> directly at the point where the error occurs, but that would potentially allow for a bunch of lock contention if multiple status updates happen at once, and they would block execution of the main threads. Using the combination of the monitor and locks</p>
<ol>
<li>allows for concurrent access by two time sensitive goroutines (updating status and updating the display)</li>
<li>reduces lock contention because write access is serialized</li>
<li>doesn’t impact the execution of the main threads, since now status updates are just sent into the background for processing instead of being called in place</li>
</ol>
<p>This seemed like the right balance to me.</p>
<p>Monitor goroutines are relatively simple to reason about and implement and a pretty essential tool to have in the eternal struggle against concurrency bugs. I’ll be quick to consider this pattern next time I’m faced with one.</p>
<h3>See also:</h3>
<ul>
<li><a href="https://medium.com/dm03514-tech-blog/golang-monitors-and-mutexes-a-light-survey-84f04f9b7c09">Monitors and Mutexes: a (light) survey</a></li>
<li><a href="https://dl.acm.org/doi/pdf/10.1145/355620.361161">Monitors: An Operating System Structuring Concept</a></li>
</ul>
]]></content>
</entry>
<entry>
<title>What's this for?</title>
<link href="https://benmuth.github.io/posts/whats-this-for.html"/>
<id>https://benmuth.github.io/posts/whats-this-for.html</id>
<updated>2023-07-18T15:39:23-05:00</updated>
<summary></summary>
<content type="html"><![CDATA[<p>To start, this blog is for me. I want a record of my projects and learnings over time. I <a href="https://herbertlui.net/dont-think-to-write-write-to-think/">write to think</a>, <a href="http://www.paulgraham.com/words.html">put ideas into words</a>, and to figure out what I really know and don’t know. I want to get better at explaining and discussing technical topics.</p>
<p>This blog might also be for you. My imagined audience is me from 6 months prior. As some background, I started programming a few years ago, and I’m going to write about things that have been interesting or confusing to me, like how a CRDT algorithm decides where to insert a character, or how WebSockets work. If that sounds interesting to you, check back soon.</p>
<p>I’m aiming for quantity and consistency. I’m not going to wait for <a href="https://ntietz.com/blog/scheduling-visits-from-the-muse/">visits from the muse</a>. Criticizing my own work comes naturally to me; producing and sharing my work with others doesn’t. I’ve been thinking of starting this blog for more than a year now, but only managed to publish my first post last month. To improve at a skill, I think that rapid iteration is more helpful than aiming for some naive idea of perfection. If I focus on quality above all, I’ll never produce, and be slow to improve.</p>
<p>I’m not trying to be novel. Trying to explain my own view of an established concept will only help my understanding. I also find value in reading the same thing being explained or discussed from multiple points of view. I don’t mind rehashing old ideas. If me from 6 months ago would have found something I wrote interesting or helpful, I consider that a success. If I focus on originality above all, I’ll never produce.</p>
<p>I’m not going to be completely correct. I’m actually counting on being wrong sometimes, and hope to be corrected. If you have feedback or want to discuss something, please email me at benmuthalaly@gmail.com. If I focus on perfect accuracy, I’ll never produce.</p>
<p>I’m trying to <a href="https://blog.tjcx.me/p/consume-less-create-more">consume less and create more</a>.</p>
<p>This is a place to practice in public. Over time, I want my posts to be of higher quality, more interesting, and more accurate. Check back in a few years to see how I did!</p>
<p>See also:
<a href="https://jvns.ca/blog/2023/06/05/some-blogging-myths/">Julia Evans – Blogging myths</a></p>
]]></content>
</entry>

</feed>