Commit d481cb2c authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Use waitgroup to shutdown connection cleanly.

parent 3af5930b
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"log" "log"
"os" "os"
"strings" "strings"
"sync"
"sync/atomic" "sync/atomic"
"time" "time"
"unsafe" "unsafe"
...@@ -40,6 +41,9 @@ type MountState struct { ...@@ -40,6 +41,9 @@ type MountState struct {
// Number of loops blocked on reading; used to control amount // Number of loops blocked on reading; used to control amount
// of concurrency. // of concurrency.
readers int32 readers int32
// Number of loops total. Needed for clean shutdown.
loops sync.WaitGroup
} }
func (ms *MountState) KernelSettings() raw.InitIn { func (ms *MountState) KernelSettings() raw.InitIn {
...@@ -174,7 +178,9 @@ func (ms *MountState) recordStats(req *request) { ...@@ -174,7 +178,9 @@ func (ms *MountState) recordStats(req *request) {
// //
// Each filesystem operation executes in a separate goroutine. // Each filesystem operation executes in a separate goroutine.
func (ms *MountState) Loop() { func (ms *MountState) Loop() {
ms.loops.Add(1)
ms.loop() ms.loop()
ms.loops.Wait()
ms.mountFile.Close() ms.mountFile.Close()
} }
...@@ -211,6 +217,7 @@ func (ms *MountState) loop() { ...@@ -211,6 +217,7 @@ func (ms *MountState) loop() {
} }
if readers <= 0 { if readers <= 0 {
ms.loops.Add(1)
go ms.loop() go ms.loop()
} }
...@@ -229,6 +236,7 @@ func (ms *MountState) loop() { ...@@ -229,6 +236,7 @@ func (ms *MountState) loop() {
} }
ms.buffers.FreeBuffer(dest) ms.buffers.FreeBuffer(dest)
ms.loops.Done()
} }
func (ms *MountState) handleRequest(req *request) { func (ms *MountState) handleRequest(req *request) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment