From 5c03407a8ae26d48cfe02b7a58e93c66784941ce Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Sat, 20 Feb 2016 02:32:12 +0100 Subject: [PATCH] Wait for store jobs to finish when exiting. This fixes #144 #147. --- edit/history.go | 4 +++- eval/builtin_func.go | 9 +++++++-- store/store.go | 7 +++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/edit/history.go b/edit/history.go index 4c2d23f3..0ddeebca 100644 --- a/edit/history.go +++ b/edit/history.go @@ -16,8 +16,10 @@ func (h *historyState) jump(i int, line string) { func (ed *Editor) appendHistory(line string) { if ed.store != nil { go func() { - ed.store.AddCmd(line) + ed.store.Waits.Add(1) // TODO(xiaq): Report possible error + ed.store.AddCmd(line) + ed.store.Waits.Done() }() } } diff --git a/eval/builtin_func.go b/eval/builtin_func.go index e47e1d8c..78ad9385 100644 --- a/eval/builtin_func.go +++ b/eval/builtin_func.go @@ -370,8 +370,13 @@ func cdInner(dir string, ec *EvalCtx) { // XXX Error ignored. pwd, err := os.Getwd() if err == nil { - // XXX Error ignored. - go ec.store.AddDir(pwd) + store := ec.store + go func() { + store.Waits.Add(1) + // XXX Error ignored. + store.AddDir(pwd) + store.Waits.Done() + }() } } } diff --git a/store/store.go b/store/store.go index 9deaa622..05274d45 100644 --- a/store/store.go +++ b/store/store.go @@ -5,13 +5,15 @@ import ( "database/sql" "fmt" "net/url" + "sync" _ "github.com/elves/go-sqlite/sqlite3" // enable the "sqlite3" SQL driver ) // Store is the permanent storage backend for elvish. type Store struct { - db *sql.DB + db *sql.DB + Waits sync.WaitGroup } var initTable = map[string](func(*sql.DB) error){} @@ -35,7 +37,7 @@ func NewStore(dbname string) (*Store, error) { // NewStoreDB creates a new Store with a custom database. The database must be // a SQLite database. func NewStoreDB(db *sql.DB) (*Store, error) { - st := &Store{db} + st := &Store{db, sync.WaitGroup{}} for name, fn := range initTable { err := fn(db) @@ -51,5 +53,6 @@ func (s *Store) Close() error { if s == nil || s.db == nil { return nil } + s.Waits.Wait() return s.db.Close() }