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

sync_worker_exec_payload: TypeError: fwrite(): Argument #2 ($data) must be of type string, null given #972

Open
675076143 opened this issue Dec 11, 2024 · 5 comments · May be fixed by laravel/framework#53872

Comments

@675076143
Copy link

Octane Version

1.5.6

Laravel Version

8.83.29

PHP Version

8.1.3

What server type are you using?

Roadrunner

Server Version

2.12.3

Database Driver & Version

No response

Description

Just response()->noContent() I will get status 500, and worker stop.

sync_worker_exec_payload: TypeError: fwrite(): Argument #2 ($data) must be of type string, null given in vendor/laminas/laminas-diactoros/src/Stream.php:234

This is because the use of declare(strict_types=1); in laminas/laminas-diactoros/src/Stream.php

So fwrite($stream, null) throw error.

And In symfony/http-foundation

When the response status is 204, the content will be set to null..😭

public function isEmpty(): bool
{
    // L1259
    return \in_array($this->statusCode, [204, 304]);
}


public function prepare(Request $request): static
{
    $headers = $this->headers;
  
    if ($this->isInformational() || $this->isEmpty()) {
        // L247
        $this->setContent(null);
   }
}

https://github.com/symfony/http-foundation/blob/7.2/Response.php#L1259

https://github.com/symfony/http-foundation/blob/7.2/Response.php#L247

Steps To Reproduce

class ExampleController extends Controller
{
    public function index()
    {
        return response()->noContent();
    }
}
@675076143
Copy link
Author

I realized I had written it wrong.

setContent has been overwritted

https://github.com/laravel/framework/blob/11.x/src/Illuminate/Http/Response.php#L51

Maybe we can copy symfony?

https://github.com/symfony/http-foundation/blob/7.2/Response.php#L420

  public function setContent($content)
  {
      // $this->original = $content;
      $this->original = $content ?? '';
 }

@675076143
Copy link
Author

Update

I also found dingo overwrite the response

'$this->original' is nullable

https://github.com/dingo/api/blob/master/src/Http/Response.php#L206

https://github.com/laravel/framework/blob/11.x/src/Illuminate/Http/ResponseTrait.php#L60

public function setContent($content)
    {
        if (! empty($content) && is_object($content) && ! $this->shouldBeJson($content)) {
           $this->original = $content; // <-----nullable

            return $this;
        }

        try {
            return parent::setContent($content); // <------do $this->content = $content ?? '';
        } catch (UnexpectedValueException $exception) {
            $this->original = $content;

            return $this;
        }
    }

@crynobone
Copy link
Member

I believe we should update Illuminate/Http/Response.php#L51 but you need to report the issue to dingo/api as we don't maintain that package.

crynobone added a commit to laravel/framework that referenced this issue Dec 13, 2024
`$content` is set to `null`

1. Symfony set the content to `null` for `response()->noContent()` https://github.com/symfony/http-foundation/blob/e88a66c3997859532bc2ddd6dd8f35aba2711744/Response.php#L246-L249
2. This cause issue with PSR-7 HTTP Stream in Octane

fixed laravel/octane#972

Signed-off-by: Mior Muhammad Zaki <[email protected]>
@crynobone
Copy link
Member

@675076143 Can you test if laravel/framework#53872 worked for you?

@675076143
Copy link
Author

@675076143 Can you test if laravel/framework#53872 worked for you?

It works 😊

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