shithub: mycel

Download patch

ref: 4174cee93fafb045b6d561d6c539bb0ff32c26de
parent: 5286f0a7643e5105f6b243081adc172b641c5caa
author: Philip Silva <philip.silva@protonmail.com>
date: Sat Jun 26 08:23:20 EDT 2021

Improved thread-safety

Mutex in logger and go routine in statusbarmsg because in can
be called inside and outside of main thread.

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -1526,20 +1526,22 @@
 		return
 	}
 
-	dui.Call <- func() {
-		if msg == "" {
-			b.StatusBar.Text = ""
-		} else {
-			b.StatusBar.Text += msg + "\n"
-		}
-		if emptyBody {
-			b.Website.UI = &duit.Label{}
-		}
+	go func() {
+		dui.Call <- func() {
+			if msg == "" {
+				b.StatusBar.Text = ""
+			} else {
+				b.StatusBar.Text += msg + "\n"
+			}
+			if emptyBody {
+				b.Website.UI = &duit.Label{}
+			}
 
-		dui.MarkLayout(dui.Top.UI)
-		dui.MarkDraw(dui.Top.UI)
-		dui.Render()
-	}
+			dui.MarkLayout(dui.Top.UI)
+			dui.MarkDraw(dui.Top.UI)
+			dui.Render()
+		}
+	}()
 }
 
 func (b *Browser) get(uri *url.URL, isNewOrigin bool) (buf []byte, contentType opossum.ContentType, err error) {
--- a/cmd/jsfs/main.go
+++ b/cmd/jsfs/main.go
@@ -14,6 +14,7 @@
 	"os"
 	"os/user"
 	"strings"
+	"sync"
 )
 
 var (
@@ -21,6 +22,7 @@
 	log *logger.Logger
 	htm string
 	js []string
+	mu sync.Mutex
 )
 
 func init() {
@@ -54,14 +56,7 @@
 	root.AddChild(c)
 	lctl := (*fs.ListenFileListener)(c)
 	go Ctl(lctl)
-	go func() {
-		err := go9p.ServeReadWriter(r, w, jsFS.Server())
-		if err != nil {
-			log.Errorf("jsfs: serve rw: %v", err)
-		}
-	}()
-
-	return
+	return go9p.ServeReadWriter(r, w, jsFS.Server())
 }
 
 func Ctl(lctl *fs.ListenFileListener) {
@@ -86,6 +81,9 @@
 		return
 	}
 	l = strings.TrimSpace(l)
+
+	mu.Lock()
+	defer mu.Unlock()
 
 	switch l {
 	case "start":
--- a/cmd/jsfs/main_test.go
+++ b/cmd/jsfs/main_test.go
@@ -20,10 +20,11 @@
 	}
 	un := u.Username
 	c1, c2 := net.Pipe()
-	err = Main(c1, c1)
-	if err != nil {
-		return
-	}
+	go func() {
+		if err = Main(c1, c1); err != nil && err != io.EOF {
+			panic(err.Error())
+		}
+	}()
 	conn, err := client.NewConn(c2)
 	if err != nil {
 		return
--- a/logger/logger.go
+++ b/logger/logger.go
@@ -5,6 +5,7 @@
 	"io"
 	goLog "log"
 	"os"
+	"sync"
 )
 
 // Sink for Go's log pkg
@@ -31,6 +32,8 @@
 
 type Logger struct {
 	Debug    bool
+
+	mu       sync.Mutex
 	last     string
 	lastSev  int
 	repeated int
@@ -71,6 +74,9 @@
 }
 
 func (l *Logger) emit(severity int, format string, v ...interface{}) {
+	l.mu.Lock()
+	defer l.mu.Unlock()
+
 	if severity == debug && !l.Debug {
 		return
 	}