Skip to content

Commit

Permalink
dynamolock/v2: fix lock race condition
Browse files Browse the repository at this point in the history
Addresses #184
  • Loading branch information
ucirello committed Mar 23, 2023
1 parent 9b891ec commit c540f30
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 4 additions & 3 deletions v2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
Expand All @@ -45,10 +44,11 @@ func TestMain(m *testing.M) {
if err != nil {
panic("cannot execute tests without Java")
}
_ = os.Remove(filepath.Join("local-dynamodb", "shared-local-instance.db")) // unconditionally remove state file
ctx, cancel := context.WithCancel(context.Background())
cmd := exec.CommandContext(ctx, javaPath, "-Djava.library.path=./DynamoDBLocal_lib", "-jar", "DynamoDBLocal.jar", "-sharedDb")
cmd := exec.CommandContext(ctx, javaPath, "-Djava.library.path=./DynamoDBLocal_lib", "-jar", "DynamoDBLocal.jar", "-sharedDb", "-inMemory")
cmd.Dir = "local-dynamodb"
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Start(); err != nil {
panic("cannot start local dynamodb:" + err.Error())
}
Expand All @@ -61,6 +61,7 @@ func TestMain(m *testing.M) {
c.Close()
break
}
time.Sleep(1 * time.Second)
exitCode := m.Run()
cancel()
cmd.Wait()
Expand Down
7 changes: 6 additions & 1 deletion v2/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ func (l *Lock) AdditionalAttributes() map[string]types.AttributeValue {
// "danger zone". It returns false if the lock has not been released and the
// lock has not yet entered the "danger zone"
func (l *Lock) IsAlmostExpired() (bool, error) {
if l == nil {
return false, ErrLockAlreadyReleased
}
l.semaphore.Lock()
defer l.semaphore.Unlock()
t, err := l.timeUntilDangerZoneEntered()
if err != nil {
return false, err
Expand All @@ -143,7 +148,7 @@ func (l *Lock) timeUntilDangerZoneEntered() (time.Duration, error) {
if l.sessionMonitor == nil {
return 0, ErrSessionMonitorNotSet
}
if l.IsExpired() {
if l.isExpired() {
return 0, ErrLockAlreadyReleased
}
return l.sessionMonitor.timeUntilLeaseEntersDangerZone(l.lookupTime), nil
Expand Down

0 comments on commit c540f30

Please sign in to comment.