linux C++ sockets - send() crashing on disconnected socket

Red Squirrel

No Lifer
May 24, 2003
69,723
13,342
126
www.betteroff.ca
I ran into some issue, I noticed send() crashes when it tries to send data if the socket got disconnected by the remote host. The worse part is theres no indication of a crash, all that happens is the program randomly exits to the command line.

How do I go about setting up some kind of error handling for this? I check the return value and all, to avoid trying to send next time, but its that first one that crashes.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
1. Enable core files (ulimit -c unlimited), that way you can examine your program's memory at exact moment it crashed out
2. Run your program in a debugger (gdb, ddd or whatever floats your boat)
3. Install signal handlers, in which you can do postmortem debugging, like dumping stack via pstack
4. USE VALGRIND

Just start with #4 and come back with a list of errors (you have to compile in debug info for valgrind to take effect).
 

Red Squirrel

No Lifer
May 24, 2003
69,723
13,342
126
www.betteroff.ca
Hmm thats a pretty sweet program, I'll play around with it, looks like I have other issues I might need to solve, which may or may not have to do with this one.

Only thing is, how do I disable it from throwing debug info even while not running the program through it? I don't want this debug info to show every time I run a custom program.
 

Red Squirrel

No Lifer
May 24, 2003
69,723
13,342
126
www.betteroff.ca
Another sockets question, how do I set sockets to non blocking, and how do I set a time out for things like send, recv and connect? Right now if I break the physical connection to the server, it sits there for ever and it continues sending packets or waiting on receives, and sits there for ages. Any other program would tell you instantly that theres a problem.

If the connection gets DCed at the software layer (such as kicking a player in a game) then it crashes like I described above. But physical disconnects just make it sit there forever.
 

Red Squirrel

No Lifer
May 24, 2003
69,723
13,342
126
www.betteroff.ca
Not sure if theres specific parameters I should add, if yes let me know and I'll redo it, but here is the output:

(the packet debug stuff is part of my own program)


[root@borg uocli]# valgrind -v ./uocli
==24519== Memcheck, a memory error detector.
==24519== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==24519== Using LibVEX rev 1732, a library for dynamic binary translation.
==24519== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==24519== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==24519== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==24519==
--24519-- Command line
--24519-- ./uocli
--24519-- Startup, with flags:
--24519-- -v
--24519-- Contents of /proc/version:
--24519-- Linux version 2.6.15-1.2054_FC5 (bhcompile@hs20-bc1-3.build.redhat.com) (gcc version 4.1.0 20060304 (Red Hat 4.1.0-3)) #1 Tue Mar 14 15:48:33 EST 2006
--24519-- Arch and hwcaps: X86, x86-sse1
--24519-- Page sizes: currently 4096, max supported 4096
--24519-- Valgrind library directory: /usr/local/lib/valgrind
--24519-- Reading syms from /lib/ld-2.4.so (0x43F000)
--24519-- Reading syms from /data/samba/shared/ryan/applications/uocli/uocli (0x8048000)
--24519-- Reading syms from /usr/local/lib/valgrind/x86-linux/memcheck (0x38000000)
--24519-- object doesn't have a dynamic symbol table
--24519-- Reading suppressions file: /usr/local/lib/valgrind/default.supp
--24519-- REDIR: 0x453B90 (index) redirected to 0x38027F4B (vgPlain_x86_linux_REDIR_FOR_index)
--24519-- Reading syms from /usr/local/lib/valgrind/x86-linux/vgpreload_core.so (0x4000000)
--24519-- Reading syms from /usr/local/lib/valgrind/x86-linux/vgpreload_memcheck.so (0x4003000)
==24519== WARNING: new redirection conflicts with existing -- ignoring it
--24519-- new: 0x00453B90 (index ) R-> 0x04006200 index
--24519-- Reading syms from /usr/lib/libstdc++.so.6.0.8 (0x89F000)
--24519-- object doesn't have a symbol table
--24519-- Reading syms from /lib/libm-2.4.so (0x597000)
--24519-- Reading syms from /lib/libgcc_s-4.1.0-20060304.so.1 (0x63C000)
--24519-- object doesn't have a symbol table
--24519-- Reading syms from /lib/libc-2.4.so (0x45C000)
--24519-- REDIR: 0x4C6360 (rindex) redirected to 0x40060E0 (rindex)
--24519-- REDIR: 0x4C71E0 (memset) redirected to 0x4006610 (memset)
--24519-- REDIR: 0x4C5FE0 (strlen) redirected to 0x4006290 (strlen)
--24519-- REDIR: 0x9564B0 (operator new(unsigned)) redirected to 0x4005C10 (operator new(unsigned))
--24519-- REDIR: 0x4C7670 (memcpy) redirected to 0x4007080 (memcpy)
--24519-- REDIR: 0x9565F0 (operator new[](unsigned)) redirected to 0x40058A0 (operator new[](unsigned))
--24519-- REDIR: 0x4C1C08 (malloc) redirected to 0x4005470 (malloc)
--24519-- REDIR: 0x954FC0 (operator delete(void*)) redirected to 0x4004DA1 (operator delete(void*))
--24519-- REDIR: 0x955020 (operator delete[](void*)) redirected to 0x4004A23 (operator delete[](void*))
--24519-- REDIR: 0x4C33A4 (free) redirected to 0x400508A (free)

Enter server address [ravager.loc]:

Enter server port [1337]:

Enter username [test]:

Enter password [test]:

--24519-- REDIR: 0x4C7240 (mempcpy) redirected to 0x4006B40 (mempcpy)
Enter Char Slot number (0-5) [0]:

Debug packets(Y/N)? [y]:

Connecting...--24519-- REDIR: 0x4C61C8 (strncmp) redirected to 0x40062F0 (strncmp)
--24519-- REDIR: 0x4C6CE0 (memchr) redirected to 0x4006480 (memchr)
--24519-- REDIR: 0x4C5940 (index) redirected to 0x40061D0 (index)
--24519-- REDIR: 0x4C5B10 (strcpy) redirected to 0x40072F0 (strcpy)
--24519-- REDIR: 0x4C7F60 (strchrnul) redirected to 0x40066B0 (strchrnul)
--24519-- REDIR: 0x4C5AB0 (strcmp) redirected to 0x4006360 (strcmp)
--24519-- REDIR: 0x4C7390 (stpcpy) redirected to 0x4006810 (stpcpy)
--24519-- REDIR: 0x4C1914 (calloc) redirected to 0x400479D (calloc)
--24519-- Reading syms from /lib/libnss_files-2.4.so (0x400B000)
--24519-- REDIR: 0x4C7E90 (rawmemchr) redirected to 0x40066E0 (rawmemchr)
--24519-- Reading syms from /lib/libnss_dns-2.4.so (0x4129000)
--24519-- Reading syms from /lib/libresolv-2.4.so (0x600000)

packet: 4 bytes
-------------------------------------------------------------------------------

0a 01 01 14
. . . .
-------------------------------------------------------------------------------


Authenticating...
first login packet: 62 bytes
-------------------------------------------------------------------------------

80 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. t e s t . . . . . . . . . . . . . . . . . . . . .

00 00 00 00 00 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . t e s t . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 5d
. . . . . . . . . ]
-------------------------------------------------------------------------------


Reveived Packet: 46 bytes
-------------------------------------------------------------------------------

a8 00 2e 5d 00 01 00 00 41 6f 56 20 54 65 73 74 00 00 00 00 00 00 00 00 00 00
. . . ] . . . . A o V T e s t . . . . . . . . . .

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 fb d4 4e 65 d0
. . . . . . . . . . . . . . . . . N e .
-------------------------------------------------------------------------------


unknown metric packet: 268 bytes
-------------------------------------------------------------------------------

d9 02 00 00 00 1c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . . . . . . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . . . . . . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . . . . . . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . . . . . . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . . . . . . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . . . . . . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . . . . . . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . . . . . . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . . . . . . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . . . . . . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00
. . . . . . . .
-------------------------------------------------------------------------------


server selection packet: 3 bytes
-------------------------------------------------------------------------------

a0 00 00
. . .
-------------------------------------------------------------------------------


Reveived Packet: 11 bytes
-------------------------------------------------------------------------------

8c d0 65 4e d4 05 39 1f 48 26 ad
. . e N . . 9 . H & .
-------------------------------------------------------------------------------

Connecting to '208.101.78.212'...
encryption key packet: 4 bytes
-------------------------------------------------------------------------------

1f 48 26 ad
. H & .
-------------------------------------------------------------------------------


post login packet: 65 bytes
-------------------------------------------------------------------------------

91 1f 48 26 ad 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. . H & . t e s t . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . . . . . t e s t . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . . . . . . . . .
-------------------------------------------------------------------------------


Picking char...
char picker packet: 73 bytes
-------------------------------------------------------------------------------

5d ed ed ed ed 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
] . . . . . . . . . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. . . . . . . . . . . . . . . . . . . . . . . . . .

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1f 48 26 ad
. . . . . . . . . . . . . . . . . . H & .
-------------------------------------------------------------------------------


Logged to world (assuming all info was correct)
Sending 0 packets...
direction: [0]:

Sequence: [0]:

move request packet: 7 bytes
-------------------------------------------------------------------------------

02 00 00 31 32 33 34
. . . 1 2 3 4
-------------------------------------------------------------------------------

Sending 1 packets...
direction: [0]:

Sequence: [1]:

move request packet: 7 bytes
-------------------------------------------------------------------------------

02 00 01 31 32 33 34
. . . 1 2 3 4
-------------------------------------------------------------------------------

Sending 2 packets...
direction: [0]:

Sequence: [2]:

move request packet: 7 bytes
-------------------------------------------------------------------------------

02 00 02 31 32 33 34
. . . 1 2 3 4
-------------------------------------------------------------------------------

Sending 3 packets...
direction: [0]: 1

Sequence: [3]:

move request packet: 7 bytes
-------------------------------------------------------------------------------

02 01 03 31 32 33 34
. . . 1 2 3 4
-------------------------------------------------------------------------------

Sending 4 packets...
direction: [1]:

Sequence: [4]:

move request packet: 7 bytes
-------------------------------------------------------------------------------

02 01 04 31 32 33 34
. . . 1 2 3 4
-------------------------------------------------------------------------------

==24519==
==24519== Process terminating with default action of signal 13 (SIGPIPE)
==24519== at 0x525311: send (in /lib/libc-2.4.so)
==24519== by 0x804F699: main (uocli.cpp:202)
--24519-- Discarding syms at 0x400B000-0x4016000 in /lib/libnss_files-2.4.so due to munmap()
--24519-- Discarding syms at 0x4129000-0x412F000 in /lib/libnss_dns-2.4.so due to munmap()
--24519-- Discarding syms at 0x600000-0x613000 in /lib/libresolv-2.4.so due to munmap()
==24519==
==24519== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 20 from 1)
--24519--
--24519-- supp: 20 Fedora-Core-5-hack3-ld24
==24519== malloc/free: in use at exit: 847 bytes in 21 blocks.
==24519== malloc/free: 518 allocs, 497 frees, 39,496 bytes allocated.
==24519==
==24519== searching for pointers to 21 not-freed blocks.
==24519== checked 92,896 bytes.
==24519==
==24519== LEAK SUMMARY:
==24519== definitely lost: 0 bytes in 0 blocks.
==24519== possibly lost: 847 bytes in 21 blocks.
==24519== still reachable: 0 bytes in 0 blocks.
==24519== suppressed: 0 bytes in 0 blocks.
==24519== Rerun with --leak-check=full to see details of leaked memory.
--24519-- memcheck: sanity checks: 5 cheap, 1 expensive
--24519-- memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use
--24519-- memcheck: auxmaps: 0 searches, 0 comparisons
--24519-- memcheck: SMs: n_issued = 19 (304k, 0M)
--24519-- memcheck: SMs: n_deissued = 0 (0k, 0M)
--24519-- memcheck: SMs: max_noaccess = 65535 (1048560k, 1023M)
--24519-- memcheck: SMs: max_undefined = 0 (0k, 0M)
--24519-- memcheck: SMs: max_defined = 35 (560k, 0M)
--24519-- memcheck: SMs: max_non_DSM = 19 (304k, 0M)
--24519-- memcheck: max sec V bit nodes: 3 (0k, 0M)
--24519-- memcheck: set_sec_vbits8 calls: 3 (new: 3, updates: 0)
--24519-- memcheck: max shadow mem size: 608k, 0M
--24519-- translate: fast SP updates identified: 6,495 ( 89.6%)
--24519-- translate: generic_known SP updates identified: 523 ( 7.2%)
--24519-- translate: generic_unknown SP updates identified: 226 ( 3.1%)
--24519-- tt/tc: 12,715 tt lookups requiring 13,245 probes
--24519-- tt/tc: 12,715 fast-cache updates, 5 flushes
--24519-- transtab: new 5,364 (117,950 -> 1,889,629; ratio 160:10) [0 scs]
--24519-- transtab: dumped 0 (0 -> ??)
--24519-- transtab: discarded 538 (10,847 -> ??)
--24519-- scheduler: 597,361 jumps (bb entries).
--24519-- scheduler: 5/9,382 major/minor sched events.
--24519-- sanity: 6 cheap, 1 expensive checks.
--24519-- exectx: 30,011 lists, 348 contexts (avg 0 per list)
--24519-- exectx: 1,035 searches, 688 full compares (664 per 1000)
--24519-- exectx: 0 cmp2, 62 cmp4, 0 cmpAll
[root@borg uocli]#




When I changed direction to 1, I had just did a disconnect on the socket at the server level, so when it tried to send (after a few tries, which should return -1 but don't for some weird reason) it just exits out of nowhere. There is a press any key prompt at the end of my program, so this is how I know its crashing. Its totally skipping that, not to mention escaping a while(1) loop. If send/recv actually properly returned values on fail then it would exit that loop gracefully, bot those functions only seem to return errors when they feel like it.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
Process terminating with default action of signal 13 (SIGPIPE)

there's no one on the other end and unless you catch it SIGPIPE will cause your app to terminate.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
How does your event loop for server look like. Are you using select(...)?

non-blocking mode

int flags = fcntl(socket, F_GETFL, 0);
fcntl(socket, F_SETFL, flags | O_NONBLOCK);
 

Red Squirrel

No Lifer
May 24, 2003
69,723
13,342
126
www.betteroff.ca
The server I did not code. How would I go about catching SIGPIPE at the client side? Figured I could check if send returns -1 but aparantly this aint working.

And how do I turn off passive valgrind debugging? its real anoying if I want to use an app normally. Since if I do ./uocli it still runs the debug stuff.