From 1d1b363fa750d15bdf6f2363d29a17028cb418a6 Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Mon, 13 Nov 2023 18:27:36 +0100 Subject: [PATCH] Remove lua.Lock Exposing locking primitives to lua plugins is tricky and may lead to deadlocks. Instead, if possible, it's better to ensure all the needed synchonization in micro itself, without leaving this burden to lua code. Since we've added micro.After() timer API and removed exposing Go timers directly to lua, now we (probably?) have no cases of lua code possibly running asynchronously without micro controlling when it is running. So now we can remove lua.Lock. This means breaking compatibility, but, until recently lua.Lock wasn't workable at all (see #2945), which suggests that it has never been really used by anyone. So it should be safe to remove it. --- cmd/micro/initlua.go | 1 - cmd/micro/micro.go | 7 ------- internal/lua/lua.go | 2 -- 3 files changed, 10 deletions(-) diff --git a/cmd/micro/initlua.go b/cmd/micro/initlua.go index 41d752cc2..77349caa5 100644 --- a/cmd/micro/initlua.go +++ b/cmd/micro/initlua.go @@ -55,7 +55,6 @@ func luaImportMicro() *lua.LTable { ulua.L.SetField(pkg, "Tabs", luar.New(ulua.L, func() *action.TabList { return action.Tabs })) - ulua.L.SetField(pkg, "Lock", luar.New(ulua.L, &ulua.Lock)) ulua.L.SetField(pkg, "After", luar.New(ulua.L, func(t time.Duration, f func()) { time.AfterFunc(t, func() { timerChan <- f diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 09c9ab2b9..303d71739 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -23,7 +23,6 @@ import ( "github.com/zyedidia/micro/v2/internal/buffer" "github.com/zyedidia/micro/v2/internal/clipboard" "github.com/zyedidia/micro/v2/internal/config" - ulua "github.com/zyedidia/micro/v2/internal/lua" "github.com/zyedidia/micro/v2/internal/screen" "github.com/zyedidia/micro/v2/internal/shell" "github.com/zyedidia/micro/v2/internal/util" @@ -418,15 +417,11 @@ func DoEvent() { select { case f := <-shell.Jobs: // If a new job has finished while running in the background we should execute the callback - ulua.Lock.Lock() f.Function(f.Output, f.Args) - ulua.Lock.Unlock() case <-config.Autosave: - ulua.Lock.Lock() for _, b := range buffer.OpenBuffers { b.Save() } - ulua.Lock.Unlock() case <-shell.CloseTerms: case event = <-screen.Events: case <-screen.DrawChan(): @@ -478,12 +473,10 @@ func DoEvent() { return } - ulua.Lock.Lock() _, resize := event.(*tcell.EventResize) if action.InfoBar.HasPrompt && !resize { action.InfoBar.HandleEvent(event) } else { action.Tabs.HandleEvent(event) } - ulua.Lock.Unlock() } diff --git a/internal/lua/lua.go b/internal/lua/lua.go index d7af16b0b..e17f2a66c 100644 --- a/internal/lua/lua.go +++ b/internal/lua/lua.go @@ -17,7 +17,6 @@ import ( "regexp" "runtime" "strings" - "sync" "time" "unicode/utf8" @@ -27,7 +26,6 @@ import ( ) var L *lua.LState -var Lock sync.Mutex // LoadFile loads a lua file func LoadFile(module string, file string, data []byte) error {