What happens to a write() or read() if....

What happens to a write() or read() if....

Post by Daniel Barro » Tue, 11 Sep 2001 05:58:04



If I have a socket on a server thats connected to from a client, and I am
in the middle of a read() or write() and the client disconnects for any
reason?

Let's say I am writing 1Kbyte to the client and it gets 500 bytes through
and the client disconnects, crashes or is physically removed from the
network?

Does it return what it really sent then when I loop to call it again it
would return <0 to show an error?

And would it be the same for a read()?

Thanks - I've just got a gap in my socket understanding.

--
Daniel Barron - use [at jadeb.com] for personal replys.
(Visit http://dansguardian.org/  - True web content filtering for all)

 
 
 

What happens to a write() or read() if....

Post by Eric P. McC » Tue, 11 Sep 2001 05:14:47



> If I have a socket on a server thats connected to from a client, and I am
> in the middle of a read() or write() and the client disconnects for any
> reason?

If you are in the middle of a read(), read() will return the number of
bytes it has read.  Subsequent read()s will return zero.

If you are in the middle of a write(), it will return the number of
bytes written.  Subsequent write()s will raise SIGPIPE or will return
EPIPE, depending on if you are blocking SIGPIPE or not.

By the way, I am sure there are several alternate reasons why read()
or write() would return a short count, so it's probably a bad idea to
take action (like by closing the socket) until you get EPIPE or a
return value of zero.

--

"I woke up this morning and realized what the game needed: pirates,
pimps, and gay furries."  - Rich "Lowtax" Kyanka

 
 
 

What happens to a write() or read() if....

Post by Daniel Barro » Tue, 11 Sep 2001 07:48:55




> > If I have a socket on a server thats connected to from a client, and I am
> > in the middle of a read() or write() and the client disconnects for any
> > reason?

> If you are in the middle of a read(), read() will return the number of
> bytes it has read.  Subsequent read()s will return zero.

> If you are in the middle of a write(), it will return the number of
> bytes written.  Subsequent write()s will raise SIGPIPE or will return
> EPIPE, depending on if you are blocking SIGPIPE or not.

> By the way, I am sure there are several alternate reasons why read()
> or write() would return a short count, so it's probably a bad idea to
> take action (like by closing the socket) until you get EPIPE or a
> return value of zero.

Thanks - that clears it up.

BTW, the reason I ask is I am trying to work out why my daemon that was
handling about 1300 concurrent users (about 50 concurrent connections at a
time) crashes or exits sometimes.  It dies with a few processes seemingly
blocked even though everything it does has a timeout.  I am doing a dry run
to work out what is happening.

This leads to another question... I've recently ported the daemon to OpenBSD
and ps aux lists what it is blocking on (select() read() etc).  That could
help me find the problem.  However, is there a way to do this on linux?

--
Daniel Barron - use [at jadeb.com] for personal replys.
(Visit http://dansguardian.org/  - True web content filtering for all)

 
 
 

What happens to a write() or read() if....

Post by Eric P. McC » Tue, 11 Sep 2001 08:22:15



> This leads to another question... I've recently ported the daemon to OpenBSD
> and ps aux lists what it is blocking on (select() read() etc).  That could
> help me find the problem.  However, is there a way to do this on linux?

There is a Linux program which lists the system calls a program is
making.  That might be useful, though as I've never used it I can't
say for sure.

--

"I woke up this morning and realized what the game needed: pirates,
pimps, and gay furries."  - Rich "Lowtax" Kyanka

 
 
 

What happens to a write() or read() if....

Post by Rene Herma » Tue, 11 Sep 2001 09:27:24



> This leads to another question... I've recently ported the daemon
> to OpenBSD and ps aux lists what it is blocking on (select()
> read() etc).  That could help me find the problem.  However, is
> there a way to do this on linux?

I believe you want the WCHAN field. "ps ax -l" shows it. You can also
have "top" show this if you type "f" for the field selection menu, and
then "u" for WCHAN.

Rene.

 
 
 

What happens to a write() or read() if....

Post by Carlos Moren » Tue, 11 Sep 2001 09:25:57



> This leads to another question... I've recently ported the daemon to OpenBSD
> and ps aux lists what it is blocking on (select() read() etc).  That could
> help me find the problem.  However, is there a way to do this on linux?

What about strace?  Not long ago, I had a similar problem, and thanks
to a kind sould in this newsgroup, the suggestion of using strace
led me to identifying the problem in practically no time!  (a call
to *write* was blocking!  It was due to clients disconnecting, and
needless to say that it took me completely by surprise -- I hadn't
suspected for a millisecond that write could block...  Of course,
after I was told the possible reasons why write could block, it
was so obvious to me...  "duh! what was I thinking?" was the only
thing that came to mind at that moment  :-))

So, to summarize:  all of the above could be put in two words:

man strace

:-)

HTH,

Carlos
--

 
 
 

What happens to a write() or read() if....

Post by Daniel Barro » Wed, 12 Sep 2001 05:54:41



[snip]

Quote:

> What about strace?  Not long ago, I had a similar problem, and thanks
> to a kind sould in this newsgroup, the suggestion of using strace
> led me to identifying the problem in practically no time!  (a call
> to *write* was blocking!  It was due to clients disconnecting, and
> needless to say that it took me completely by surprise -- I hadn't
> suspected for a millisecond that write could block...  Of course,
> after I was told the possible reasons why write could block, it
> was so obvious to me...  "duh! what was I thinking?" was the only
> thing that came to mind at that moment  :-))

> So, to summarize:  all of the above could be put in two words:

> man strace

Looks really useful.  Thanks.  Bit tricky using with a daemon process that
forks though.

write() blocking?  I thought it would block only if the output buffer was
full?  And if the client disconnects that it would return immediately?
Is this not the case?

What /are/ the possible reasons why write() can block?

--
Daniel Barron - use [at jadeb.com] for personal replys.
(Visit http://dansguardian.org/  - True web content filtering for all)

 
 
 

What happens to a write() or read() if....

Post by Daniel Barro » Wed, 12 Sep 2001 05:56:56



[snip]

Quote:

> I believe you want the WCHAN field. "ps ax -l" shows it. You can also
> have "top" show this if you type "f" for the field selection menu, and
> then "u" for WCHAN.

> Rene.

Thanks that's REALLY useful!  I expect it'll lead me to the problem.  Cheers!

--
Daniel Barron - use [at jadeb.com] for personal replys.
(Visit http://dansguardian.org/  - True web content filtering for all)

 
 
 

What happens to a write() or read() if....

Post by Kevin Lacquemen » Wed, 12 Sep 2001 10:51:23


[...]

    DB> Looks really useful.  Thanks.  Bit tricky using with a daemon process that
    DB> forks though.

You could have conditionally compile your daemonizing code.  That way
when you need to strace it you just define NO_DAEMONIZE or something
similar.

[...]

Cheers,
Kevin

--
He that breaks a thing to find out what it is has left the path of
wisdom                                         -- Gandalf the Grey

 
 
 

What happens to a write() or read() if....

Post by Ben Hutching » Wed, 12 Sep 2001 19:56:17




<snip>
> > So, to summarize:  all of the above could be put in two words:

> > man strace

> Looks really useful.  Thanks.  Bit tricky using with a daemon process that
> forks though.

<snip>

I'm told this is bad practice, though that may be a matter of debate.
However, the -f option causes strace to follow forks, so it's not a
problem here.

 
 
 

What happens to a write() or read() if....

Post by Eric Taylo » Tue, 05 Feb 2002 07:00:25


I don't think this is likely the answer to your problem, but there is one
thing that can happen here that I believe is a problem with tcp in general,
but I can't find any solution to:

If you have a read() going, and there is a hardware glitch that causes the
other side to disconnect because it cannot get an ack back, then if the line does not recover
in a few seconds, you will never be informed that the other side disconnnected.
Then your read() will hang forever.

This is easily repeated, just pull the connection from the network for 20 seconds,
a little test that every fault tollerant program should be able to recover from.

Just thought I'd mention that one, since I too have been trying to build a very
robust server and ran into this problem, and have no answer.

eric


> If I have a socket on a server thats connected to from a client, and I am
> in the middle of a read() or write() and the client disconnects for any
> reason?

> Let's say I am writing 1Kbyte to the client and it gets 500 bytes through
> and the client disconnects, crashes or is physically removed from the
> network?

> Does it return what it really sent then when I loop to call it again it
> would return <0 to show an error?

> And would it be the same for a read()?

> Thanks - I've just got a gap in my socket understanding.

> --
> Daniel Barron - use [at jadeb.com] for personal replys.
> (Visit http://dansguardian.org/  - True web content filtering for all)

 
 
 

What happens to a write() or read() if....

Post by Chorbala » Wed, 06 Feb 2002 01:11:30


Quote:

>> If I have a socket on a server thats connected to from a client, and I am
>> in the middle of a read() or write() and the client disconnects for any
>> reason?

>> Let's say I am writing 1Kbyte to the client and it gets 500 bytes through
>> and the client disconnects, crashes or is physically removed from the
>> network?

>> Does it return what it really sent then when I loop to call it again it
>> would return <0 to show an error?

>> And would it be the same for a read()?

>> Thanks - I've just got a gap in my socket understanding.

As long as there was something to read, the read() would read it and
return the number of bytes actually read. Then if you loop, it MAY fail,
but it may not, too.  If it doesn't  fail and blocking mode was selected,
it would block... so it is a good idea to set non-blocking mode on reads..
Blocking mode is the default..
 
 
 

What happens to a write() or read() if....

Post by Stuart Lamb » Wed, 06 Feb 2002 06:21:05



>As long as there was something to read, the read() would read it and
>return the number of bytes actually read. Then if you loop, it MAY fail,
>but it may not, too.  If it doesn't  fail and blocking mode was selected,
>it would block... so it is a good idea to set non-blocking mode on reads..

Either that, or use select() or poll() to verify that there is data
available for reading prior to using read().

--
"You didn't slay the dragon?!"
"It's on my to-do list, now come on!"
  -- Shrek.

 
 
 

1. Sockets: write() writes OK, yet read() reads garbage...?

Strange thing happens. There is a TCP socket established between A and B. Some
information flows OK between the two. However, one particular sequence of
communication does not. A writes a command to B, and immediately goes into
read() for a responce. B does something with the message and writes back to A a
struct, with some fields set to important values. When A reads it (as the same
struct - both use the same header file), however, it contains garbage. All the
fiels are set to 0 (zero). And it reads the correct size of the struct:

  n = read( sock, &preamble, sizeof(preamble) );
  if( n < 0 || n > sizeof(preamble) )
   error...

What/where can the problem be? Does A (or maybe B) have to flush the socket
after it wrote a command, to make sure it does not read its own packet back?

Thanks.
--
Simon   B-)>

2. Configuring linuxconf

3. What happened to IFS?

4. Help! Funai 2X cdrom & creative adapter

5. ksh/sh difference w/IFS or read??

6. st.conf parameters required for Sony Tape Recorder

7. script to read a line in an array with , as IFS

8. Has anyone managed to compile xbiff++ for solaris

9. OS/2 IFS for reading EXT2FS?

10. Question on writing c program implement cp function using UNIX system calls - read, write, etc.

11. Need help writing C program using UNIX system calls (read, write, etc) that copies files

12. Read & Read/Write Groups

13. How to share read-only to one client and read-write to all others?