peachpiecompiler / peachpie

PeachPie - the PHP compiler and runtime for .NET and .NET Core
https://www.peachpie.io
Apache License 2.0
2.31k stars 202 forks source link

stream_select seems to be bad #1011

Closed avriltank closed 2 years ago

avriltank commented 2 years ago
Unhandled exception. System.ArgumentOutOfRangeException: Number must be either non-negative and less than or equal to Int32.MaxValue or -1. (Parameter 'millisecondsTimeout')
Actual value was -2147483648.
   at System.Threading.Thread.Sleep(Int32 )
   at Pchp.Library.Streams.PhpStreams.stream_select(PhpArray& read, PhpArray& write, PhpArray& except, Int32 tv_sec, Int32 tv_usec)
   at serverworker.events.Select.loop()
   at CallSite.Target(Closure , CallSite , PhpValue , Context )
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1)
   at serverworker.Worker.runOnly()
   at serverworker.Worker.run()
   at CallSite.Target(Closure , CallSite , PhpValue , Context , CallerTypeParam )
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid3[T0,T1,T2](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at <Root>.test_php.<Main>(Context <ctx>, PhpArray <locals>, Object this, RuntimeTypeHandle <self>)
   at <Script>.Main(String[] args)

I read the peachpie code,it should be right,but in my case,the error occurs after timer run a few times.

                // check stream available or timeout
                if (count != 0 || (wait_ms > 0 && (System.DateTime.UtcNow - startTime).TotalMilliseconds >= wait_ms))
                {
                    // update ref parameters and return:
                    read = readcheck;
                    write = writecheck;
                    except = errcheck;

                    //
                    return count;
                }

                // avoids polling CPU without a break:
                if (i < 8)
                    // just spin
                    Thread.Yield();
                else
                    //the peachpie code  should be right
                    // sleep the thread for [2..100] ms
                    Thread.Sleep(Math.Min(Math.Min((i + 1) * 2, 100), wait_ms));
avriltank commented 2 years ago

serverworker.zip

Surprisingly, this test case can run perfectly for 24 hours in php7 and 8, and errors will occur after about 10 minutes in peachpie

jakubmisek commented 2 years ago

thanks! is it expected to wait for the stream_select for a longer period of time (hours) ?

avriltank commented 2 years ago

Yes,error happens aftera longer period of time.Now it is fixed well.Otherway,the resouce id maked by function stream_create_server should be the handle of socket.

jakubmisek commented 2 years ago

thanks @avriltank

avriltank commented 2 years ago

Thanks for your work for this great project,is there any branch for other people to edit code for peachpie

jakubmisek commented 2 years ago

thank you! Yes anyone can "fork" the project here on GitHub, and create pull requests.