Skip to content

Panic on concurrent Set and Close #13

@ajgajg1134

Description

@ajgajg1134

Hello, I recently ran into an issue where there is a panic writing to a closed channel here.

I was able to create a quick test to reproduce this:

func TestSetAfterCloseNoPanic(t *testing.T) {
	c, err := newTestCache()
	require.NoError(t, err)
	require.NotNil(t, c)

	go c.Set(1, 1, 1)
	go c.Close()
	time.Sleep(time.Millisecond * 100)
}

On my side I'm working around this issue by just coordinating my go routines better here so that I wait for all my possible "Set" calls to complete before calling Close. So if anyone else runs into this, that's the easy way to get around it currently.

It seems like this is unintentional as there's a check here if the cache has been closed, however if the calls are concurrent it's possible for them to be ordered such that:

  1. The Set() call begins and verifies the Cache has not been closed
  2. Close() is called and the setBuf channel is closed
  3. Set() attempts to write to setBuf triggering a panic

If this is considered incorrect usage of the library API feel free to close this issue, although I might recommend a small docs note on Close indicating that it must not be called concurrently with Set

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions