You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by yu...@apache.org on 2025/05/06 05:15:46 UTC

(opendal) 01/01: feat(bindings/go): proper cleanup in Writer.Close()

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

yuchanns pushed a commit to branch 6128
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit 17b1be5d9bc83c6c6339e00d938c8d2f4e086c6f
Author: Hanchin Hsieh <me...@yuchanns.xyz>
AuthorDate: Tue May 6 13:15:10 2025 +0800

    feat(bindings/go): proper cleanup in Writer.Close()
    
    Ensure resources are properly freed and closed in Writer.Close() implementation by:
    - Adding withWriterClose FFI binding
    - Deferring free after close to prevent resource leaks
    - Properly propagating close errors
---
 bindings/go/ffi.go   |  1 +
 bindings/go/write.go | 24 ++++++++++++++++++++++--
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/bindings/go/ffi.go b/bindings/go/ffi.go
index 4cca16170..fb55b5157 100644
--- a/bindings/go/ffi.go
+++ b/bindings/go/ffi.go
@@ -143,4 +143,5 @@ var withFFIs = []contextWithFFI{
 	withOperatorWriter,
 	withWriterWrite,
 	withWriterFree,
+	withWriterClose,
 }
diff --git a/bindings/go/write.go b/bindings/go/write.go
index a426c9eae..0461ab7fe 100644
--- a/bindings/go/write.go
+++ b/bindings/go/write.go
@@ -182,8 +182,9 @@ func (w *Writer) Write(p []byte) (n int, err error) {
 // properly flushed and written to the storage. Otherwise, the data may be lost.
 func (w *Writer) Close() error {
 	free := getFFI[writerFree](w.ctx, symWriterFree)
-	free(w.inner)
-	return nil
+	defer free(w.inner)
+	close := getFFI[writerClose](w.ctx, symWriterClose)
+	return close(w.inner)
 }
 
 var _ io.WriteCloser = (*Writer)(nil)
@@ -305,3 +306,22 @@ var withWriterWrite = withFFI(ffiOpts{
 		return int(result.size), nil
 	}
 })
+
+const symWriterClose = "opendal_writer_close"
+
+type writerClose func(r *opendalWriter) error
+
+var withWriterClose = withFFI(ffiOpts{
+	sym:    symWriterClose,
+	rType:  &ffi.TypePointer,
+	aTypes: []*ffi.Type{&ffi.TypePointer},
+}, func(ctx context.Context, ffiCall func(rValue unsafe.Pointer, aValues ...unsafe.Pointer)) writerClose {
+	return func(r *opendalWriter) error {
+		var e *opendalError
+		ffiCall(
+			unsafe.Pointer(&e),
+			unsafe.Pointer(&r),
+		)
+		return parseError(ctx, e)
+	}
+})