-
Notifications
You must be signed in to change notification settings - Fork 5
feat: fail fast when two chatwoot processes mutate the same conversation #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
15a740d
feat: add per-conversation file lock package
scmmishra e361d89
feat: fail fast when another process is mutating the same conversation
scmmishra ce6e1c9
feat: guard conv open, pending, and unassign with the per-conversatio…
scmmishra 6e22e02
feat: guard conv label with the per-conversation lock
scmmishra 9e08ea1
feat: guard conv priority with the per-conversation lock
scmmishra 61c210f
fix: acquire the conversation lock before resolving the agent on assign
scmmishra 03c1f2d
chore: bump Go to 1.26.5 for the GO-2026-5856 crypto/tls fix
scmmishra 999086e
chore: satisfy errcheck on the lock acquire failure path
scmmishra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/chatwoot/cli/internal/lock" | ||
| ) | ||
|
|
||
| // Mutating conversation commands must fail fast when another process holds the | ||
| // per-conversation lock. The App is empty on purpose: the commands must bail | ||
| // out before touching the API client. | ||
| func TestConvMutationsFailWhenConversationLocked(t *testing.T) { | ||
| home := t.TempDir() | ||
| t.Setenv("HOME", home) // unix | ||
| t.Setenv("USERPROFILE", home) // windows | ||
|
|
||
| const convID = 555 | ||
| lk, err := lock.AcquireConversation(convID) | ||
| if err != nil { | ||
| t.Fatalf("AcquireConversation: %v", err) | ||
| } | ||
| defer lk.Release() | ||
|
|
||
| app := &App{} | ||
| cases := []struct { | ||
| name string | ||
| run func() error | ||
| }{ | ||
| {"reply", func() error { return (&ConvReplyCmd{ID: convID, Text: "hi"}).Run(app) }}, | ||
| {"resolve", func() error { return (&ConvResolveCmd{ID: convID}).Run(app) }}, | ||
| {"open", func() error { return (&ConvOpenCmd{ID: convID}).Run(app) }}, | ||
| {"pending", func() error { return (&ConvPendingCmd{ID: convID}).Run(app) }}, | ||
| {"assign", func() error { return (&ConvAssignCmd{ID: convID, Team: 1}).Run(app) }}, | ||
| // By-name assign: the lock must win before resolveAgent's API lookup. | ||
| {"assign-by-name", func() error { return (&ConvAssignCmd{ID: convID, Agent: "jane"}).Run(app) }}, | ||
| {"unassign", func() error { return (&ConvUnassignCmd{ID: convID}).Run(app) }}, | ||
| {"snooze", func() error { return (&ConvSnoozeCmd{ID: convID}).Run(app) }}, | ||
| {"label", func() error { return (&ConvLabelCmd{ID: convID, Labels: []string{"vip"}}).Run(app) }}, | ||
| {"priority", func() error { return (&ConvPriorityCmd{ID: convID, Level: "high"}).Run(app) }}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| err := tc.run() | ||
| if err == nil { | ||
| t.Fatal("want error while conversation is locked, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), "already running on this conversation") { | ||
| t.Errorf("error = %q, want lock-held message", err) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Package lock provides per-conversation advisory file locks so that | ||
| // concurrent chatwoot processes on the same machine don't run mutating | ||
| // commands (e.g. reply) against the same conversation at once. | ||
| package lock | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strconv" | ||
|
|
||
| "github.com/chatwoot/cli/internal/config" | ||
| ) | ||
|
|
||
| // ErrLocked is returned when another process already holds the lock. | ||
| var ErrLocked = errors.New("conversation is locked by another chatwoot process") | ||
|
|
||
| // Lock is a held per-conversation lock. Release it with Release. | ||
| // The OS releases it automatically if the process exits or crashes. | ||
| type Lock struct { | ||
| f *os.File | ||
| } | ||
|
|
||
| // AcquireConversation takes an exclusive non-blocking lock for the given | ||
| // conversation ID. It returns ErrLocked (wrapped) if another process holds it. | ||
| func AcquireConversation(id int) (*Lock, error) { | ||
| cfgDir, err := config.ConfigDir() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return acquireAt(filepath.Join(cfgDir, "locks"), id) | ||
| } | ||
|
|
||
| func acquireAt(dir string, id int) (*Lock, error) { | ||
| if err := os.MkdirAll(dir, 0o700); err != nil { | ||
| return nil, err | ||
| } | ||
| path := filepath.Join(dir, fmt.Sprintf("conv-%d.lock", id)) | ||
| f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := tryLock(f); err != nil { | ||
| _ = f.Close() | ||
| return nil, fmt.Errorf("conversation %d: %w", id, err) | ||
| } | ||
| // Best-effort PID marker for debugging; the flock is the real lock. | ||
| _ = f.Truncate(0) | ||
| _, _ = f.WriteString(strconv.Itoa(os.Getpid()) + "\n") | ||
| return &Lock{f: f}, nil | ||
| } | ||
|
|
||
| // Release unlocks and closes the lock file. The file itself is left in place: | ||
| // unlinking it would race with other processes locking the same path. | ||
| func (l *Lock) Release() { | ||
| if l == nil || l.f == nil { | ||
| return | ||
| } | ||
| _ = unlock(l.f) | ||
| _ = l.f.Close() | ||
| l.f = nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package lock | ||
|
|
||
| import ( | ||
| "errors" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestAcquireConflictAndRelease(t *testing.T) { | ||
| dir := filepath.Join(t.TempDir(), "locks") | ||
|
|
||
| l1, err := acquireAt(dir, 42) | ||
| if err != nil { | ||
| t.Fatalf("first acquire: %v", err) | ||
| } | ||
|
|
||
| if _, err := acquireAt(dir, 42); !errors.Is(err, ErrLocked) { | ||
| t.Fatalf("second acquire: want ErrLocked, got %v", err) | ||
| } | ||
|
|
||
| // A different conversation is unaffected. | ||
| l2, err := acquireAt(dir, 43) | ||
| if err != nil { | ||
| t.Fatalf("acquire other conversation: %v", err) | ||
| } | ||
| l2.Release() | ||
|
|
||
| l1.Release() | ||
| l3, err := acquireAt(dir, 42) | ||
| if err != nil { | ||
| t.Fatalf("reacquire after release: %v", err) | ||
| } | ||
| l3.Release() | ||
| } | ||
|
|
||
| func TestReleaseIsIdempotent(t *testing.T) { | ||
| l, err := acquireAt(filepath.Join(t.TempDir(), "locks"), 1) | ||
| if err != nil { | ||
| t.Fatalf("acquire: %v", err) | ||
| } | ||
| l.Release() | ||
| l.Release() // must not panic | ||
| var nilLock *Lock | ||
| nilLock.Release() // nil receiver must be safe | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.