From 7437dcc9e9bdc0f754df62a487848f8d5774101c Mon Sep 17 00:00:00 2001 From: Pavel Vorobyov Date: Fri, 27 Sep 2019 17:08:28 +0300 Subject: [PATCH] threadsafe acquiring pty --- remote/remote.go | 3 +++ remote/runcmd.go | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/remote/remote.go b/remote/remote.go index 3f74a67..588e6d4 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "path/filepath" + "sync" "time" ) @@ -21,6 +22,7 @@ var ( currentRemoteTmpdir string currentDebug bool outputFile *os.File + ptyLock *sync.Mutex noneInterpreter string suInterpreter string @@ -30,6 +32,7 @@ var ( // Initialize initializes new execution pool func Initialize(numThreads int, username string) { pool = NewPool(numThreads) + ptyLock = new(sync.Mutex) SetUser(username) SetPassword("") SetRaise(RTNone) diff --git a/remote/runcmd.go b/remote/runcmd.go index 4ec9842..67ea525 100644 --- a/remote/runcmd.go +++ b/remote/runcmd.go @@ -19,24 +19,32 @@ func (w *Worker) runcmd(task *Task) int { n int password string passwordSent bool + ptmx *os.File + fd *poller.FD ) cmd := createSSHCmd(task.Hostname, task.Cmd) cmd.Env = append(os.Environ(), environment...) - ptmx, err := pty.Start(cmd) + // threadsafe acquiring necessary file descriptors + ptyLock.Lock() + ptmx, err = pty.Start(cmd) if err != nil { log.Debugf("WRK[%d]: Error creating ptmx: %v", w.id, err) + ptyLock.Unlock() return ErrTerminalError } defer ptmx.Close() - fd, err := poller.NewFD(int(ptmx.Fd())) + fd, err = poller.NewFD(int(ptmx.Fd())) if err != nil { log.Debugf("WRK[%d]: Error creating poller FD: %v", w.id, err) + ptyLock.Unlock() return ErrTerminalError } defer fd.Close() + ptyLock.Unlock() + // threadsafe acquiring necessary file descriptors ends buf := make([]byte, bufferSize) taskForceStopped := false