Progress: Trying to fix more deadlocks, failing.

This commit is contained in:
Andrey Petrov 2016-07-12 18:24:02 -04:00
parent ea2d4d0dfc
commit 0fdeda8b75
3 changed files with 24 additions and 18 deletions

27
host.go
View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"strings" "strings"
"sync"
"time" "time"
"github.com/shazow/rateio" "github.com/shazow/rateio"
@ -30,16 +31,17 @@ type Host struct {
*chat.Room *chat.Room
listener *sshd.SSHListener listener *sshd.SSHListener
commands chat.Commands commands chat.Commands
auth *Auth
motd string
auth *Auth
count int
// Version string to print on /version // Version string to print on /version
Version string Version string
// Default theme // Default theme
theme message.Theme theme message.Theme
mu sync.Mutex
motd string
count int
} }
// NewHost creates a Host on top of an existing listener. // NewHost creates a Host on top of an existing listener.
@ -63,12 +65,16 @@ func NewHost(listener *sshd.SSHListener, auth *Auth) *Host {
// SetTheme sets the default theme for the host. // SetTheme sets the default theme for the host.
func (h *Host) SetTheme(theme message.Theme) { func (h *Host) SetTheme(theme message.Theme) {
h.mu.Lock()
h.theme = theme h.theme = theme
h.mu.Unlock()
} }
// SetMotd sets the host's message of the day. // SetMotd sets the host's message of the day.
func (h *Host) SetMotd(motd string) { func (h *Host) SetMotd(motd string) {
h.mu.Lock()
h.motd = motd h.motd = motd
h.mu.Unlock()
} }
func (h Host) isOp(conn sshd.Connection) bool { func (h Host) isOp(conn sshd.Connection) bool {
@ -91,15 +97,21 @@ func (h *Host) Connect(term *sshd.Terminal) {
}() }()
defer user.Close() defer user.Close()
h.mu.Lock()
motd := h.motd
count := h.count
h.count++
h.mu.Unlock()
// Send MOTD // Send MOTD
if h.motd != "" { if motd != "" {
user.Send(message.NewAnnounceMsg(h.motd)) go user.Send(message.NewAnnounceMsg(motd))
} }
member, err := h.Join(user) member, err := h.Join(user)
if err != nil { if err != nil {
// Try again... // Try again...
id.SetName(fmt.Sprintf("Guest%d", h.count)) id.SetName(fmt.Sprintf("Guest%d", count))
member, err = h.Join(user) member, err = h.Join(user)
} }
if err != nil { if err != nil {
@ -111,7 +123,6 @@ func (h *Host) Connect(term *sshd.Terminal) {
term.SetPrompt(GetPrompt(user)) term.SetPrompt(GetPrompt(user))
term.AutoCompleteCallback = h.AutoCompleteFunction(user) term.AutoCompleteCallback = h.AutoCompleteFunction(user)
user.SetHighlight(user.Name()) user.SetHighlight(user.Name())
h.count++
// Should the user be op'd on join? // Should the user be op'd on join?
if h.isOp(term.Conn) { if h.isOp(term.Conn) {

View File

@ -8,7 +8,6 @@ import (
"io/ioutil" "io/ioutil"
"strings" "strings"
"testing" "testing"
"time"
"github.com/shazow/ssh-chat/chat/message" "github.com/shazow/ssh-chat/chat/message"
"github.com/shazow/ssh-chat/sshd" "github.com/shazow/ssh-chat/sshd"
@ -215,9 +214,5 @@ func TestHostKick(t *testing.T) {
close(done) close(done)
}() }()
select { <-done
case <-done:
case <-time.After(time.Second * 1):
t.Fatal("Timeout.")
}
} }

View File

@ -46,15 +46,12 @@ func (l *SSHListener) ServeTerminal() <-chan *Terminal {
ch := make(chan *Terminal) ch := make(chan *Terminal)
go func() { go func() {
defer l.Close()
defer close(ch)
for { for {
conn, err := l.Accept() conn, err := l.Accept()
if err != nil { if err != nil {
logger.Printf("Failed to accept connection: %v", err) logger.Printf("Failed to accept connection: %v", err)
return break
} }
// Goroutineify to resume accepting sockets early // Goroutineify to resume accepting sockets early
@ -67,6 +64,9 @@ func (l *SSHListener) ServeTerminal() <-chan *Terminal {
ch <- term ch <- term
}() }()
} }
l.Close()
close(ch)
}() }()
return ch return ch