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

Using Fibers causes epic crash #46

Open
withinboredom opened this issue Oct 20, 2022 · 11 comments · May be fixed by #387 or #171
Open

Using Fibers causes epic crash #46

withinboredom opened this issue Oct 20, 2022 · 11 comments · May be fixed by #387 or #171
Labels
bug Something isn't working

Comments

@withinboredom
Copy link
Collaborator

withinboredom commented Oct 20, 2022

Minimal code to reproduce:

<?php

do {
    $running = false;
    //$running = frankenphp_handle_request(function (): void {
        $fiber = new Fiber(function() {
            echo "Starting Fiber\n";
        });
        $fiber->start();
    //});
} while ($running);

With some slight modifications, it can also be reproduced in worker mode.

@dunglas dunglas added the bug Something isn't working label Oct 21, 2022
krakjoe added a commit to krakjoe/frankenphp that referenced this issue Nov 10, 2022
  This is the bare minimum required to make fibers work within the go
  runtime.
krakjoe added a commit to krakjoe/frankenphp that referenced this issue Nov 10, 2022
  This is the bare minimum required to make fibers work within the go
  runtime.
krakjoe added a commit to krakjoe/frankenphp that referenced this issue Nov 10, 2022
  This is the bare minimum required to make fibers work within the go
  runtime.
dunglas pushed a commit that referenced this issue Aug 4, 2023
  This is the bare minimum required to make fibers work within the go
  runtime.
@dunglas dunglas linked a pull request Aug 4, 2023 that will close this issue
@dunglas dunglas pinned this issue Aug 6, 2023
dunglas pushed a commit that referenced this issue Aug 16, 2023
  This is the bare minimum required to make fibers work within the go
  runtime.
dunglas pushed a commit that referenced this issue Sep 8, 2023
  This is the bare minimum required to make fibers work within the go
  runtime.
@withinboredom
Copy link
Collaborator Author

withinboredom commented Dec 13, 2023

@dunglas the following Docker file (props @cdaguerre in #374) appears to "fix" fibers. At least for this reproducer with manual testing. It needs more testing:

FROM dunglas/frankenphp:latest-builder-php8.3-alpine AS builder

COPY --from=caddy:builder-alpine /usr/bin/xcaddy /usr/bin/xcaddy

ENV CGO_ENABLED=1 XCADDY_SETCAP=1 CGO_CXXFLAGS=-fPIE CGO_CFLAGS=-fPIE CGO_LDFLAGS=-pie XCADDY_GO_BUILD_FLAGS='-buildmode=pie -ldflags="-w -s" -trimpath'
RUN xcaddy build \
    --output /usr/local/bin/frankenphp \
    --with github.com/dunglas/frankenphp=./ \
    --with github.com/dunglas/frankenphp/caddy=./caddy/ \
    --with github.com/dunglas/mercure/caddy \
    --with github.com/dunglas/vulcain/caddy

🥳 🤞 🤞 still testing...

@dunglas
Copy link
Owner

dunglas commented Dec 13, 2023

Great news! Don't hesitate to open a PR with this changes, so we can see if this fix the issue for all architectures.

@withinboredom
Copy link
Collaborator Author

I'll do some proper testing by Monday (by updating the fiber branch), but I haven't seen a crash yet via manual testing.

@piotrekkr
Copy link

@withinboredom I had issues with fibers so I could also test this on my Cloud Run service but not really sure where can I get docker image to use with this fix.

@withinboredom
Copy link
Collaborator Author

withinboredom commented Jan 10, 2024

It doesn't fix it, per se, more-or-less just reduces the probability of a crash.

Edit to add: the best way to prevent a crash is to just not output anything at all inside a fiber.

dunglas pushed a commit that referenced this issue Jan 14, 2024
  This is the bare minimum required to make fibers work within the go
  runtime.
@erikfrerejean
Copy link

I've just encountered this issue and using the workaround from @withinboredom did resolve the exception. In this project the culprit seem to be the monolog logger as that is the only place fibers are being used.

@withinboredom
Copy link
Collaborator Author

I started working on a cgo library several weeks ago to allow output from c to go without calling go. It's still a wip: https://github.com/withinboredom/cgoc

There's a segfault once the number of concurrent requests gets high (due to usage of some C synchronization primitives from go), and a memory leak, but the it's pretty fast by itself (~8gbs on my machine).

I hope to have it working sometime in the next few months as a potential solution.

@dunglas
Copy link
Owner

dunglas commented Jul 9, 2024

@withinboredom IMHO the best option would be to fix the issue directly in Go!

dunglas pushed a commit that referenced this issue Jul 9, 2024
  This is the bare minimum required to make fibers work within the go
  runtime.
@withinboredom
Copy link
Collaborator Author

@dunglas I highly doubt it will ever be fixable, for very valid reasons. The reason it is failing boils down to the following:

  1. C creates a new thread
  2. C calls go_handle_request (ncgo = 1)
  3. Go calls frankenphp_execute_script (reenter C)
  4. PHP creates a fiber
  5. C calls Go (go_ub_write for example) (ncgo = 2)
  6. crash as designed

According to the CL (https://go-review.googlesource.com/c/go/+/530480) this means changing the stack for an ncgo > 1 will never be possible -- for very valid safety reasons. This was a huge part of my approach in taking over Go threads (ncgo <= 1 always).

If we can fix the ncgo issue, then we are free to muck around with the stack as much as we want.

@withinboredom
Copy link
Collaborator Author

withinboredom commented Jul 9, 2024

One way to fix it might be to have go_handle_request return a pointer that we can continue with (making ncgo = 0), then continuing in C to frankenphp_execute_script, so if a fiber is created, and we call things like go_ub_write, ncgo == 1 and it will just reset the stack bounds just fine (in theory).

@dunglas
Copy link
Owner

dunglas commented Jul 9, 2024

According to golang/go#62130 (comment), this seems fixable directly in Go for our case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants