]> git.r.bdr.sh - rbdr/mobius/commitdiff
Fix some data races
authorJeff Halter <redacted>
Sun, 5 Jun 2022 22:27:54 +0000 (15:27 -0700)
committerJeff Halter <redacted>
Sun, 5 Jun 2022 22:27:54 +0000 (15:27 -0700)
hotline/client_conn.go
hotline/server.go
hotline/transaction.go
hotline/transaction_handlers.go

index 4a471b3382b4533160bc62d89fc0afc5a54280f4..65e18eead5ea526b963f9515486ec55cc8d8e42b 100644 (file)
@@ -151,7 +151,7 @@ func (cc *ClientConn) Authorize(access int) bool {
 }
 
 // Disconnect notifies other clients that a client has disconnected
 }
 
 // Disconnect notifies other clients that a client has disconnected
-func (cc ClientConn) Disconnect() {
+func (cc *ClientConn) Disconnect() {
        cc.Server.mux.Lock()
        defer cc.Server.mux.Unlock()
 
        cc.Server.mux.Lock()
        defer cc.Server.mux.Unlock()
 
@@ -165,7 +165,7 @@ func (cc ClientConn) Disconnect() {
 }
 
 // notifyOthers sends transaction t to other clients connected to the server
 }
 
 // notifyOthers sends transaction t to other clients connected to the server
-func (cc ClientConn) notifyOthers(t Transaction) {
+func (cc *ClientConn) notifyOthers(t Transaction) {
        for _, c := range sortedClients(cc.Server.Clients) {
                if c.ID != cc.ID && c.Agreed {
                        t.clientID = c.ID
        for _, c := range sortedClients(cc.Server.Clients) {
                if c.ID != cc.ID && c.Agreed {
                        t.clientID = c.ID
index 5ff88420a331372ca33b3a092739ee0436411b28..cf022fc9f9e3a2d3a6649253f1256e371cb079a3 100644 (file)
@@ -646,28 +646,42 @@ const dlFldrActionNextFile = 3
 // handleFileTransfer receives a client net.Conn from the file transfer server, performs the requested transfer type, then closes the connection
 func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
        defer func() {
 // handleFileTransfer receives a client net.Conn from the file transfer server, performs the requested transfer type, then closes the connection
 func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
        defer func() {
+
                if err := conn.Close(); err != nil {
                        s.Logger.Errorw("error closing connection", "error", err)
                }
        }()
 
                if err := conn.Close(); err != nil {
                        s.Logger.Errorw("error closing connection", "error", err)
                }
        }()
 
+       defer func() {
+               if r := recover(); r != nil {
+                       fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
+                       s.Logger.Errorw("PANIC", "err", r, "trace", string(debug.Stack()))
+               }
+       }()
+
        txBuf := make([]byte, 16)
        txBuf := make([]byte, 16)
-       _, err := conn.Read(txBuf)
-       if err != nil {
+       if _, err := io.ReadFull(conn, txBuf); err != nil {
                return err
        }
 
        var t transfer
                return err
        }
 
        var t transfer
-       _, err = t.Write(txBuf)
-       if err != nil {
+       if _, err := t.Write(txBuf); err != nil {
                return err
        }
 
        transferRefNum := binary.BigEndian.Uint32(t.ReferenceNumber[:])
                return err
        }
 
        transferRefNum := binary.BigEndian.Uint32(t.ReferenceNumber[:])
-       fileTransfer := s.FileTransfers[transferRefNum]
+       defer func() {
+               s.mux.Lock()
+               delete(s.FileTransfers, transferRefNum)
+               s.mux.Unlock()
+       }()
 
 
-       // delete single use transferRefNum
-       delete(s.FileTransfers, transferRefNum)
+       s.mux.Lock()
+       fileTransfer, ok := s.FileTransfers[transferRefNum]
+       s.mux.Unlock()
+       if !ok {
+               return errors.New("invalid transaction ID")
+       }
 
        switch fileTransfer.Type {
        case FileDownload:
 
        switch fileTransfer.Type {
        case FileDownload:
index 04fb4b6520cb51a884ca4f7b86e9c208870d6716..e7bfa58318a9f1baa4330e14ae5709ee55da3546 100644 (file)
@@ -214,7 +214,7 @@ func ReadFields(paramCount []byte, buf []byte) ([]Field, error) {
        return fields, nil
 }
 
        return fields, nil
 }
 
-func (t Transaction) MarshalBinary() (data []byte, err error) {
+func (t *Transaction) MarshalBinary() (data []byte, err error) {
        payloadSize := t.Size()
 
        fieldCount := make([]byte, 2)
        payloadSize := t.Size()
 
        fieldCount := make([]byte, 2)
@@ -238,7 +238,7 @@ func (t Transaction) MarshalBinary() (data []byte, err error) {
 }
 
 // Size returns the total size of the transaction payload
 }
 
 // Size returns the total size of the transaction payload
-func (t Transaction) Size() []byte {
+func (t *Transaction) Size() []byte {
        bs := make([]byte, 4)
 
        fieldSize := 0
        bs := make([]byte, 4)
 
        fieldSize := 0
@@ -251,7 +251,7 @@ func (t Transaction) Size() []byte {
        return bs
 }
 
        return bs
 }
 
-func (t Transaction) GetField(id int) Field {
+func (t *Transaction) GetField(id int) Field {
        for _, field := range t.Fields {
                if id == int(binary.BigEndian.Uint16(field.ID)) {
                        return field
        for _, field := range t.Fields {
                if id == int(binary.BigEndian.Uint16(field.ID)) {
                        return field
index 4a8683101d1ca45e490c61d342838d0b59336a08..abe2c54489dd124fb63fd2be0f034d2323300576 100644 (file)
@@ -1521,12 +1521,14 @@ func HandleUploadFile(cc *ClientConn, t *Transaction) (res []Transaction, err er
        transactionRef := cc.Server.NewTransactionRef()
        data := binary.BigEndian.Uint32(transactionRef)
 
        transactionRef := cc.Server.NewTransactionRef()
        data := binary.BigEndian.Uint32(transactionRef)
 
+       cc.Server.mux.Lock()
        cc.Server.FileTransfers[data] = &FileTransfer{
                FileName:        fileName,
                FilePath:        filePath,
                ReferenceNumber: transactionRef,
                Type:            FileUpload,
        }
        cc.Server.FileTransfers[data] = &FileTransfer{
                FileName:        fileName,
                FilePath:        filePath,
                ReferenceNumber: transactionRef,
                Type:            FileUpload,
        }
+       cc.Server.mux.Unlock()
 
        replyT := cc.NewReply(t, NewField(fieldRefNum, transactionRef))
 
 
        replyT := cc.NewReply(t, NewField(fieldRefNum, transactionRef))