DEV Community

Marcus Beauregard
Marcus Beauregard

Posted on

A mutex is enough. Until the check and the write are in different locks.

tinyurl is a URL shortener I wrote in Go. One file, 60 lines, a map behind a sync.RWMutex. When I wrote it I felt good about the concurrency. The shared state was guarded. There were no naked map reads. I had done the thing you are supposed to do.

Then I read it back a week later and found a race, an ignored crypto error, and a small memory leak. The mutex was never the problem. The problem was that the mutex was in the wrong place.

The map, as written

type store struct {
    mu sync.RWMutex
    m  map[string]string
}

func (s *store) put(code, url string) { s.mu.Lock(); s.m[code] = url; s.mu.Unlock() }

func (s *store) get(code string) (string, bool) {
    s.mu.RLock(); defer s.mu.RUnlock()
    u, ok := s.m[code]
    return u, ok
}
Enter fullscreen mode Exit fullscreen mode

Every access goes through a method. Every method locks. Grep for s.m[ and you find two hits, both inside a critical section. It looks airtight, and for a single key operation it is.

The invariant that spans two calls

A short code has to map to exactly one URL, permanently. That is an invariant about two operations, and my safe primitives only covered one each. The natural next version of the handler looks like this:

code := shortCode()
if _, exists := s.get(code); exists { // lock 1
    code = shortCode() // still not safe
}
s.put(code, req["url"]) // lock 2
Enter fullscreen mode Exit fullscreen mode

Two goroutines can both call get, both see "free", both release the read lock, and both put the same code. Last writer wins. The first person's link now silently points at someone else's URL — and that is the good case, where the code was eight hex characters. With a broken rand.Read returning zeros it is guaranteed.

Two locks do not make one atomic operation. A RWMutex protects memory, not meaning.

The fix is not a bigger lock scattered across the handler. It is moving the whole invariant into one method:

func (s *store) create(url string) (string, bool) {
    s.mu.Lock()
    defer s.mu.Unlock()
    for i := 0; i < 8; i++ {
        code := shortCode()
        if _, taken := s.m[code]; taken {
            continue
        }
        s.m[code] = url
        return code, true
    }
    return "", false
}
Enter fullscreen mode Exit fullscreen mode

Now "check" and "insert" live in the same critical section, so the window does not exist. The retry loop means a collision costs one iteration instead of a lost link, and returning false after eight tries gives the handler something honest to do instead of pretending it worked.

The ignored error

crypto/rand failing sounds theoretical until you remember rand.Read returns an error precisely because the kernel CSPRNG can be unavailable: early boot, a container with no entropy, a locked-down sandbox. My original:

b := make([]byte, 4)
rand.Read(b)
return fmt.Sprintf("%x", b)
Enter fullscreen mode Exit fullscreen mode

If Read fails, b stays zeroed and every code is 00000000. The first request wins and every link after it points at that same URL. This is the failure mode I keep finding in my own code: the error path does not crash, it produces a plausible-looking wrong answer.

func shortCode() (string, error) {
    b := make([]byte, 4)
    if _, err := rand.Read(b); err != nil {
        return "", fmt.Errorf("shortCode: %w", err)
    }
    return hex.EncodeToString(b), nil
}
Enter fullscreen mode Exit fullscreen mode

Six lines of error handling to turn "all my links go to one place" into a 500 with a reason in the log.

The other two, quickly

json.NewDecoder(r.Body).Decode(&req) was unhandled as well, so POST / with {} stored an empty string and returned a short link that redirects to "" — a client-side loop for anything that follows it. One if err != nil and a 400 fixes it.

And the map only grows. No delete, no TTL, no cap, so one entry per request until the process dies. For a toy that is acceptable; the point is that I had called it finished. http.ListenAndServe(":8080", ...) also ignores its error, which is why the process "started fine" the one time the port was already taken.

What I actually learned

The useful move was not "add more locking". It was writing the invariant as one sentence — a code maps to one URL, permanently — and then asking where that sentence could be broken. It could be broken between two correctly locked methods. That is where the bug lived.

A mutex is usually enough. But it only protects what is inside it, so put the whole sentence inside, not half of it.

Top comments (1)

Collapse
 
kashif_manzer profile image
Kashif Manzer •

The part that makes this easy to miss is that go test -race would not catch the original version. Every map access is locked, so there is no data race, only a broken invariant spread across two calls. A test that hammers create from many goroutines and then checks that each code still points at its own URL would catch it, and that check is worth keeping alongside the fix. It is a good reminder that a clean race detector run is not proof the logic is safe.