You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2026/05/17 17:34:12 UTC

(opendal) 01/01: fix(binding/go): keep service library loaded

This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch xuanwo/fix-go-library-unload
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit c8279c25d369668dfcd47f760418df7e848e4337
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Mon May 18 01:33:57 2026 +0800

    fix(binding/go): keep service library loaded
---
 bindings/go/ffi.go | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/bindings/go/ffi.go b/bindings/go/ffi.go
index 376663600..6d44ef33f 100644
--- a/bindings/go/ffi.go
+++ b/bindings/go/ffi.go
@@ -22,6 +22,7 @@ package opendal
 import (
 	"context"
 	"errors"
+	"sync"
 	"unsafe"
 
 	"github.com/jupiterrider/ffi"
@@ -84,8 +85,29 @@ func (f *FFI[T]) withFFI(ctx context.Context, lib uintptr) (context.Context, err
 
 var withFFIs []contextWithFFI
 
-func newContext(path string) (ctx context.Context, cancel context.CancelFunc, err error) {
+var (
+	libraryMu sync.Mutex
+	libraries = map[string]uintptr{}
+)
+
+func loadSharedLibrary(path string) (uintptr, error) {
+	libraryMu.Lock()
+	defer libraryMu.Unlock()
+
+	if lib, ok := libraries[path]; ok {
+		return lib, nil
+	}
+
 	lib, err := LoadLibrary(path)
+	if err != nil {
+		return 0, err
+	}
+	libraries[path] = lib
+	return lib, nil
+}
+
+func newContext(path string) (ctx context.Context, cancel context.CancelFunc, err error) {
+	lib, err := loadSharedLibrary(path)
 	if err != nil {
 		return
 	}
@@ -96,9 +118,10 @@ func newContext(path string) (ctx context.Context, cancel context.CancelFunc, er
 			return
 		}
 	}
-	cancel = func() {
-		_ = FreeLibrary(lib)
-	}
+	// Keep the library loaded for the process lifetime. The context stores libffi
+	// call targets into this library, so unloading it during operator cleanup can
+	// leave Go frames returning into unmapped code.
+	cancel = func() {}
 
 	return
 }