GNU bug report logs - #58498
Foreign callback returned by procedure->pointer cannot be executed in foreign thread.

Previous Next

Package: guile;

Reported by: Zhu Zihao <all_but_last <at> 163.com>

Date: Thu, 13 Oct 2022 16:23:02 UTC

Severity: normal

To reply to this bug, email your comments to 58498 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-guile <at> gnu.org:
bug#58498; Package guile. (Thu, 13 Oct 2022 16:23:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Zhu Zihao <all_but_last <at> 163.com>:
New bug report received and forwarded. Copy sent to bug-guile <at> gnu.org. (Thu, 13 Oct 2022 16:23:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Zhu Zihao <all_but_last <at> 163.com>
To: bug-guile <at> gnu.org
Subject: Foreign callback returned by procedure->pointer cannot be executed
 in foreign thread.
Date: Fri, 14 Oct 2022 00:01:08 +0800
[Message part 1 (text/plain, inline)]
The foreign function pointer returned by "procedure->pointer" can only
executed in a thread which Scheme context is already initialized.

This may not so convenient because foreign library may create its own
thread and run our callback in foreign thread.

To show what will happen, this is a small example using Guile FFI & pthread API
to create a foreign thread and run foreign callback in it.

```
(begin
  (use-modules (rnrs bytevectors)
               (system foreign)
               (system foreign-library))

  (define libc
    (load-foreign-library #f))

  (define pthread_create
    (foreign-library-function libc "pthread_create"
                              #:return-type int
                              #:arg-types `(* * * *)))

  (define pthread-handle-ret
    (make-bytevector 4))

  (define thread-runner
    (procedure->pointer '* (lambda (_)
                             (display "I' m from a foreign thread!\n")
                             %null-pointer)
                        `(*)))

  (pthread_create (bytevector->pointer pthread-handle-ret)
                  %null-pointer
                  thread-runner
                  %null-pointer))
```

In Guile 3.0.8. This will cause segfault and crash. My patch to fix this
problem is attached below. It wraps the real execution of foreign
callback in "scm_with_guile".

[signature.asc (application/pgp-signature, inline)]
[0001-Allow-closure-returned-by-procedure-pointer-executed.patch (text/x-patch, inline)]
From c6ede90552019a5851e4ba0de41fa9dfd3269b38 Mon Sep 17 00:00:00 2001
From: Zhu Zihao <all_but_last <at> 163.com>
Date: Fri, 14 Oct 2022 00:00:32 +0800
Subject: [PATCH] Allow closure returned by procedure->pointer executed in
 foreign thread.

* libguile/foreign.c (invoke_closure): Move the core logci to
"do_invoke_closure". Wrap it by "scm_with_guile".
(do_invoke_closure): New function.
---
 libguile/foreign.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/libguile/foreign.c b/libguile/foreign.c
index 1f594b0e4..2f9a8469a 100644
--- a/libguile/foreign.c
+++ b/libguile/foreign.c
@@ -1148,13 +1148,19 @@ scm_i_foreign_call (SCM cif_scm, SCM pointer_scm, int *errno_ret,
 
 #ifdef FFI_CLOSURES
 
-/* Trampoline to invoke a libffi closure that wraps a Scheme
-   procedure.  */
-static void
-invoke_closure (ffi_cif *cif, void *ret, void **args, void *data)
+static void *
+do_invoke_closure (void *outer_data)
 {
+  ffi_cif *cif;
+  void *ret, **args, *data;
   size_t i;
   SCM proc, *argv, result;
+  void ** outer_args = (void **) outer_data;
+
+  cif = outer_args[0];
+  ret = outer_args[1];
+  args = outer_args[2];
+  data = outer_args[3];
 
   proc = SCM_PACK_POINTER (data);
 
@@ -1167,6 +1173,21 @@ invoke_closure (ffi_cif *cif, void *ret, void **args, void *data)
   result = scm_call_n (proc, argv, cif->nargs);
 
   unpack (cif->rtype, ret, result, 1);
+
+  return NULL;
+}
+
+/* Trampoline to invoke a libffi closure that wraps a Scheme
+   procedure.  */
+static void
+invoke_closure (ffi_cif *cif, void *ret, void **args, void *data)
+{
+  void *outer_args[4] = { cif, ret, args, data };
+
+  /* Foreign code may call this Scheme closure in a context which Guile is not
+     initialized. */
+  scm_with_guile (do_invoke_closure, outer_args);
+
 }
 
 SCM_DEFINE (scm_procedure_to_pointer, "procedure->pointer", 3, 0, 0,
-- 
2.38.0

[Message part 4 (text/plain, inline)]
-- 
Retrieve my PGP public key:

  gpg --recv-keys B3EBC086AB0EBC0F45E0B4D433DB374BCEE4D9DC

Zihao

Information forwarded to bug-guile <at> gnu.org:
bug#58498; Package guile. (Tue, 24 Jan 2023 13:22:01 GMT) Full text and rfc822 format available.

Message #8 received at 58498 <at> debbugs.gnu.org (full text, mbox):

From: Zhu Zihao <all_but_last <at> 163.com>
To: 58498 <at> debbugs.gnu.org
Subject: Re: bug#58498: Acknowledgement (Foreign callback returned by
 procedure->pointer cannot be executed in foreign thread.)
Date: Tue, 24 Jan 2023 21:20:46 +0800
Ping.

Can someone hear me?
-- 
Retrieve my PGP public key:

  gpg --recv-keys B3EBC086AB0EBC0F45E0B4D433DB374BCEE4D9DC

Zihao





This bug report was last modified 1 year and 339 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.