Skip to content

Commit

Permalink
Fix hang queue on open due to panic on atomic op
Browse files Browse the repository at this point in the history
The atomic transaction counter needs to be aligned to 64bit words, so to
not cause a panic on some architectures (arm or 32bit x86)

Unfortunately the file lock was not released when this panic occured,
making applications hang on startup.

We move the atomic to the top of the file and also ensure the file lock
is correctly released (using defer) if an error or panic occurs on
transaction begin.
  • Loading branch information
urso committed Jan 20, 2019
1 parent 6ea1d41 commit 939e016
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ import (
// pages of type PageSize. Pages within the file are only accessible by page IDs
// from within active transactions.
type File struct {
// Atomic fields.
// Do not move: Must be 64bit-word aligned on some architectures.
txids uint64

observer Observer

path string
Expand All @@ -59,8 +63,6 @@ type File struct {
metaActive int

stats FileStats

txids uint64
}

// internal contants
Expand Down Expand Up @@ -336,15 +338,25 @@ func (f *File) beginTx(settings TxOptions) (*Tx, reason) {
}

tracef("request new transaction (readonly: %v)\n", settings.Readonly)

// Acquire transaction log.
// Unlock on panic, so applications will not be blocked in case they try to
// defer some close operations on the file.
ok := false
lock := f.locks.TxLock(settings.Readonly)
lock.Lock()
tracef("init new transaction (readonly: %v)\n", settings.Readonly)
defer cleanup.IfNot(&ok, lock.Unlock)

txid := atomic.AddUint64(&f.txids, 1)

tracef("init new transaction (readonly: %v)\n", settings.Readonly)

tx := newTx(f, txid, lock, settings)
tracef("begin transaction: %p (readonly: %v)\n", tx, settings.Readonly)

tx.onBegin()

ok = true
return tx, nil
}

Expand Down

0 comments on commit 939e016

Please sign in to comment.