如何prevent SIGPIPE或prevent从结束的服务器? [英] How to prevent SIGPIPE or prevent the server from ending?

查看:239
本文介绍了如何prevent SIGPIPE或prevent从结束的服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用相当标准的C ++ TCP服务器程序的的pthreads 绑定的和的接受的。我有场景,在服务器端(阅读:崩溃)。当我杀了一个连接的客户端

原因坠毁是在的文件的失败,从而使程序接收到一个SIGPIPE的的write()电话。我猜,这使得服务器退出。

我想,当然,未处理的信号,意味着退出,所以让我们使用信号()

 信号(SIGPIPE,SIG_IGN);

因为从 2人写采取:


  

EPIPE连接FD 的管道或插座,其读出端是封闭的。当发生这种情况的写作过程中也将获得一个SIGPIPE信号。 (因此,写入返回值被视为仅当程序捕获,块或忽略该信号。)


唉,没有。既不在服务器线程,也没有客户线程这个问题似乎有所帮助。

因此​​,我怎么prevent的的write()呼叫从提高的信号,或(以务实)怎么办我从离开停止服务器。


我的诊断是:


  • 服务器线程启动,装订,听力,接受。

  • 让客户端连接(通过telnet为例)

  • 发送 pkill的远程登录崩溃客户端

不必要的行为:服务器退出,在​​ GDB 的用

  ...在写()在../sysdeps/unix/syscall-template.S:82
82 T_PSEUDO(SYSCALL_SYMBOL,SYSCALL_NAME,SYSCALL_NARGS)

回溯的:

 #0 ...写()在../sysdeps/unix/syscall-template.S:82
#1 ...在ClientHandler的:: MESG(标准::字符串)()
#2 ...在ClientHandler的手柄::()()
300:#3 ...在start_thread在pthread_create.c(ARG =<价值优化掉了&GT)
#4 ...在克隆()在../sysdeps/unix/sysv/linux/x86_64/clone.S:112
#5 ...在?? ()


解决方案

万一做不到信号你有没有之前产卵掉任何线程忽略?如果等到以后,另外一个线程仍然可以拿起信号,并退出你的应用程序。

如果不这样做,你总是可以做一个写调查 / 选择试图写入前确保插座是可写的。

A quite standard C++ TCP server program using pthreads, bind, listen and accept. I have the scenario that the server ends (read: crashes) when I kill a connected client.

The reason for the crash is that the write() call on the file fails, thus the program receives a SIGPIPE. And I guess, this makes the server exit.

I thought, "of course, unhandled signal means exit", so let's use signal():

signal(SIGPIPE, SIG_IGN);

because, taken from man 2 write:

EPIPE fd is connected to a pipe or socket whose reading end is closed. When this happens the writing process will also receive a SIGPIPE signal. (Thus, the write return value is seen only if the program catches, blocks or ignores this signal.)

Alas, no. Neither in the server thread nor the client threads does this seem to help.

So, how do I prevent the write() call from raising that signal, or (to be pragmatic) how do I stop the server from exiting.


My diagnostics are:

  • server thread started, binding, listening, accepting.
  • let a client connect (via telnet for example)
  • send a pkill telnet to crash the client

unwanted behavior: server exits, in gdb with

... in write () at ../sysdeps/unix/syscall-template.S:82
82      T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS)

and the backtrace:

#0  ... in write () at ../sysdeps/unix/syscall-template.S:82
#1  ... in ClientHandler::mesg(std::string) ()
#2  ... in ClientHandler::handle() ()
#3  ... in start_thread (arg=<value optimized out>) at pthread_create.c:300
#4  ... in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112
#5  ... in ?? ()

解决方案

Did you by any chance not do the signal ignore prior to spawning off any threads? If you waited until later one of the other threads could still pick up the signal and exit your app.

If that doesn't do it, you can always do a write poll/select before trying the write to make sure the socket is writable.

这篇关于如何prevent SIGPIPE或prevent从结束的服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆