Azure / DotNetty

DotNetty project – a port of netty, event-driven asynchronous network application framework
Other
4.09k stars 977 forks source link

Very strange behavior after new a task on echo example #236

Closed zxarcg closed 7 years ago

zxarcg commented 7 years ago

I only create a test class, code is as followed, then in EchoServerHandler class , add TestUserThread _thread = new TestUserThread(); then server always can't receive client message。Remove last line , i can receive message。but if i use "System.Threading.Thread thread = new System.Threading.Thread(SendMessage);..", i can receive message. Too strange, please help me.

public class TestUserThread { public TestUserThread() { Task.Factory.StartNew(SendMessage, TaskCreationOptions.LongRunning);

        //System.Threading.Thread thread = new System.Threading.Thread(SendMessage);
        //thread.IsBackground = true;
        //a.Start();
    }
    void SendMessage(){
         while(true){ Thread.Sleep(100);}
     }

}

nayato commented 7 years ago

if you could put it out (let's say in a branch of your fork) I could check it out. It's hard to understand the problem without the context.

zxarcg commented 7 years ago

hi nayato,

please view https://github.com/zxarcg/DotNetty

zxarcg commented 7 years ago

hi nayato,

Through debug, i found reason. because in "bool RunAllTasks(PreciseTimeSpan timeout) in SingleThreadEventExecutor.cs" , task.Run() is synchronous, and my task action code is " while(true)..", so next tasks always can't run . so i only exit from task's while clause , EchoServerHandler ChannelRead method can be called.

is it a bug ?

i think maybe it's reason is netty 4.x thread model, all read/write is executed serial by netty nioeventloop, and my business task is wrapped by netty task, and is executed by serial, so my business task impact io read /write.

nayato commented 7 years ago

It certainly isn't DotNetty's bug. You are expected not to block the event loop and that's exactly what you do with while (true). You may still maintain the tight loop semantics if you'd like by scheduling the same method to event loop through IEventExecutor.Execute(..).