Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

12
Unexpected block when opening named pipe
Post Flair (click to view more posts with a particular flair)
Post Body

I have the following code on Linux:

local shpipe=io.popen("sh -s","w")
shpipe:setvbuf("no")
local round=0
while true do
        shpipe:write("echo test > fifo\n")
        shpipe:flush()
        print("Before")
        local fifoh=io.open("fifo","r")
        print("After")
        local rd=fifoh:read("*l")
        fifoh:close()
        round=round 1
        print(round)
end

Before you run the code make sure you run "mkfifo fifo" in your current directory as this code expects a named pipe with that name.

You will notice that the code gets randomly stuck (after a random number of iterations) while trying to simply open the named pipe.

I know that working with pipes is tricky but I don't see why it would block there. Checking the process tree it seems that, when the problem occurs, "sh -s" was probably more slow than usual in trying to start up "echo". But I don't see why this would be a problem. Opening a named pipe which is not opened by someone on the other side should block the process that tries to open it. I even tested it with two lua processes and this is the case, the one who tried to open for read access was blocked until the other lua process tried to open the named pipe for write access. And vice versa. In other words, whether my script is the first to open its end of the pipe, or sh/echo is first, it shouldn't matter. But the code above shows that it does matter and I don't have a clue why.

My question to any Linux gurus here is: If the Lua process gets blocked on IO, like it does, is there ANY reason for child processes to be paused or denying CPU time? Based on what I know, no, children should keep running, sh should keep running and echo should run but maybe I'm wrong. If children pause when the parent blocks on IO then it would explain why echo never starts up, never opens the other side of the named pipe, and the parent then would block forever.

I tried to further delay the execution of "echo" and hopefully the opening of the pipe for write by adding a "sleep 1;" right before echo but it doesn't make the deadlock certain even though Lua should be opening the pipe before sh does. So.... it's not about the order of who opens the pipe? If that's not the problem then what else could be different? Why else would sh/echo decide to never open the named pipe?

Author
Account Strength
100%
Account Age
17 years
Verified Email
Yes
Verified Flair
No
Total Karma
13,905
Link Karma
2,072
Comment Karma
11,818
Profile updated: 3 days ago
Posts updated: 10 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago