Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #105: Ensure the callback fiber is always alive as long as the event loop lives #107

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

bwoebi
Copy link
Contributor

@bwoebi bwoebi commented Mar 12, 2025

Different proposed fix compared to #106.

@bwoebi
Copy link
Contributor Author

bwoebi commented Mar 12, 2025

Might be enough to simply check for it at the start of run()...

@bwoebi
Copy link
Contributor Author

bwoebi commented Mar 13, 2025

Ah, interesting. With other event loops this fails, because the event loop resource is destroyed.

@trowski
Copy link
Member

trowski commented Mar 13, 2025

That and you can't suspend in a destructor in PHP < 8.4

@bwoebi
Copy link
Contributor Author

bwoebi commented Mar 13, 2025

I'm not sure whether we can do anything about that - it's destructors in the extension... At least for ext-uv once the destructor is called, it's not usable anymore.

@danog
Copy link
Contributor

danog commented Mar 13, 2025

Re-initializing the event loop handles helps:

diff --git a/src/EventLoop/Driver/EvDriver.php b/src/EventLoop/Driver/EvDriver.php
index e0a33a3..3f46e45 100644
--- a/src/EventLoop/Driver/EvDriver.php
+++ b/src/EventLoop/Driver/EvDriver.php
@@ -91,6 +91,8 @@ final class EvDriver extends AbstractDriver
         // We need to clear all references to events manually, see
         // https://bitbucket.org/osmanov/pecl-ev/issues/31/segfault-in-ev_timer_stop
         $this->events = [];
+
+        $this->handle = new \EvLoop();
     }

     /**
diff --git a/src/EventLoop/Driver/EventDriver.php b/src/EventLoop/Driver/EventDriver.php
index 869c3a9..7327560 100644
--- a/src/EventLoop/Driver/EventDriver.php
+++ b/src/EventLoop/Driver/EventDriver.php
@@ -93,6 +93,9 @@ final class EventDriver extends AbstractDriver
             $this->handle->free();
             unset($this->handle);
         }
+
+        /** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
+        $this->handle = new \EventBase();
     }

     /**
diff --git a/src/EventLoop/Driver/UvDriver.php b/src/EventLoop/Driver/UvDriver.php
index f46d909..dfd6e63 100644
--- a/src/EventLoop/Driver/UvDriver.php
+++ b/src/EventLoop/Driver/UvDriver.php
@@ -80,6 +80,11 @@ final class UvDriver extends AbstractDriver
         };
     }

+    public function __destruct()
+    {
+        $this->handle = \uv_loop_new();
+    }
+
     /**
      * {@inheritdoc}
      */

This may or may not cause a leak (only) in the EventDriver due to https://bitbucket.org/osmanov/pecl-event/issues/50/garbage-collection-issue

@danog
Copy link
Contributor

danog commented Mar 16, 2025

Ping @trowski :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants