Code Monkey home page Code Monkey logo

Comments (16)

trowski avatar trowski commented on May 23, 2024

fwrite emits a warning when attempting a write and the buffer is full. I'm not aware of any way to avoid that problem.

Do we need the feof check at all? It seems unreliable at best, which is why we started counting empty writes in the first place.

from byte-stream.

brstgt avatar brstgt commented on May 23, 2024

It seems like this works pretty well:

            set_error_handler(function ($errno, $msg, $file, $line) {
                // Skip EAGAIN errors
                if (strpos($msg, "errno=11") !== false) {
                    return;
                }
                throw new StreamException("Error writing to stream: $msg ($errno)", $errno);
            });

            try {
                // Use conditional, because PHP doesn't like getting null passed
                if ($this->chunkSize) {
                    $written = \fwrite($this->resource, $data, $this->chunkSize);
                } else {
                    $written = \fwrite($this->resource, $data);
                }
            }
            finally {
                restore_error_handler();
            }

from byte-stream.

trowski avatar trowski commented on May 23, 2024

What happens if you only delete the feof and don't bother with the error handler? Does it just keep waiting to write then?

from byte-stream.

brstgt avatar brstgt commented on May 23, 2024

There was no error, so it was fine but i guess the proper way should be to handle fwrite errors, shouldn't it?

from byte-stream.

trowski avatar trowski commented on May 23, 2024

I'm just trying to understand why feof hangs, but fwrite succeeds. The custom error handler is probably fine. I think EAGAIN writes are uncommon enough that the performance difference will be negligible.

from byte-stream.

trowski avatar trowski commented on May 23, 2024

On macOS EAGAIN seems to equal 35 rather than 11. PHP has some extension specific constants that expose this number (PCNTL_EAGAIN, SOCKET_EAGAIN, and MSG_EAGAIN). Will have to look if there's another way to find this value.

from byte-stream.

trowski avatar trowski commented on May 23, 2024

Using strpos("Resource temporarily unavailable") also works for me. Can I depend on that error message though?

Edit: Looks like no, that is also an OS-dependent message.

Removing the feof check from the initial write in ResourceOutputStream::send() causes the test testClosedRemoteSocketWithFork to fail, so I'm not sure we can remove that entirely.

from byte-stream.

brstgt avatar brstgt commented on May 23, 2024

Does it fail also with the error handler?

I get temporarily unavailable on Linux but i first thought errno=11 would be safer. It shouldnt be a big deal to have a platform dependent check, right?

from byte-stream.

kelunik avatar kelunik commented on May 23, 2024

Platform dependent checks are always ugly. Error number will be better than message, because these messages can be localized.

from byte-stream.

trowski avatar trowski commented on May 23, 2024

@brstgt The test still fails with the error handler.

Maybe we should be looking at why feof is hanging on a non-blocking stream.

from byte-stream.

kelunik avatar kelunik commented on May 23, 2024

I encountered hanging feof resulting in StreamException("The stream was closed by the peer").

If it results in a StreamException, it doesn't hang. Could you clarify what hang means?

from byte-stream.

brstgt avatar brstgt commented on May 23, 2024

from byte-stream.

kelunik avatar kelunik commented on May 23, 2024

The only thing I recall that might result in hanging are fragmented TLS records (packets), but we have never verified that.

from byte-stream.

kelunik avatar kelunik commented on May 23, 2024

Seems like https://github.com/php/php-src/blob/9a497f5c10ee547234b678208875ef3490870bb7/ext/openssl/xp_ssl.c#L2475-L2476 is the problem that results in the hang.

from byte-stream.

kelunik avatar kelunik commented on May 23, 2024

See php/php-src#3729 for a fix.

from byte-stream.

kelunik avatar kelunik commented on May 23, 2024

The latest PHP releases are fixed now.

from byte-stream.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.