diff options
| author | Paul Buetow <pbuetow@mimecast.com> | 2020-02-29 17:27:10 +0000 |
|---|---|---|
| committer | Paul Buetow <pbuetow@mimecast.com> | 2020-02-29 17:27:10 +0000 |
| commit | 1d096119505b2eca99ff445644cce94ac0d8b3b8 (patch) | |
| tree | d2c8b4e8f539b4af69bd71540facb11cb2a0924f /internal/server | |
| parent | 7911b102171309dfc43bc2faccac6de9e490f175 (diff) | |
race condition
Diffstat (limited to 'internal/server')
| -rw-r--r-- | internal/server/background/background.go | 17 | ||||
| -rw-r--r-- | internal/server/handlers/serverhandler.go | 2 |
2 files changed, 16 insertions, 3 deletions
diff --git a/internal/server/background/background.go b/internal/server/background/background.go index 51ef052..225b82a 100644 --- a/internal/server/background/background.go +++ b/internal/server/background/background.go @@ -33,6 +33,7 @@ func New() Background { // Add a background job. func (b Background) Add(userName, jobName string, cancel context.CancelFunc, wg *sync.WaitGroup) error { key := b.key(userName, jobName) + logger.Debug("background", "Add", key) b.mutex.Lock() defer b.mutex.Unlock() @@ -54,17 +55,25 @@ func (b Background) Add(userName, jobName string, cancel context.CancelFunc, wg // Cancel a background job. func (b Background) Cancel(userName, jobName string) error { - return b.cancel(b.key(userName, jobName)) + key := b.key(userName, jobName) + logger.Debug("background", "Cancel", key) + + return b.cancel(key) } func (b Background) cancel(key string) error { job, ok := b.get(key) + logger.Debug("background", "cancel", key, job, ok) + if !ok { return errors.New("no job to cancel") } + logger.Debug("background", "cancel", "run job.cancel()") job.cancel() + logger.Debug("background", "cancel", "run job.wg.Wait()") job.wg.Wait() + logger.Debug("background", "cancel", "run b.delete(key)") b.delete(key) return nil @@ -72,6 +81,8 @@ func (b Background) cancel(key string) error { // ListJobsC returns a channel listing all jobs of the given user. func (b Background) ListJobsC(userName string) <-chan string { + logger.Debug("background", "ListJobC", userName) + ch := make(chan string) go func() { @@ -92,6 +103,8 @@ func (b Background) ListJobsC(userName string) <-chan string { } func (b Background) get(key string) (job, bool) { + logger.Debug("background", "get", key) + b.mutex.Lock() defer b.mutex.Unlock() @@ -100,6 +113,8 @@ func (b Background) get(key string) (job, bool) { } func (b Background) delete(key string) { + logger.Debug("background", "delete", key) + b.mutex.Lock() defer b.mutex.Unlock() diff --git a/internal/server/handlers/serverhandler.go b/internal/server/handlers/serverhandler.go index 819cddd..b638e69 100644 --- a/internal/server/handlers/serverhandler.go +++ b/internal/server/handlers/serverhandler.go @@ -250,7 +250,6 @@ func (h *ServerHandler) handleUserCommand(ctx context.Context, argc int, args [] splitted := strings.Split(args[0], ":") command := splitted[0] - // TODO: Refactor: Create an "options" clase, combine makeOptions and readOptions there. options, err := readOptions(splitted[1:]) if err != nil { h.sendServerMessage(logger.Error(h.user, err)) @@ -332,7 +331,6 @@ func (h *ServerHandler) handleUserCommand(ctx context.Context, argc int, args [] if background { commandCtx, cancel := context.WithCancel(h.serverCtx) - // TODO: For background jobs dont attempt to send data to dtail client as there might be no SSH connection if err := h.background.Add(h.user.Name, jobName, cancel, &wg); err != nil { h.sendServerMessage(logger.Error(h.user, err, jobName, args)) finished() |
