我有一个小型服务器程序,它接受TCP或本地UNIX套接字上的连接,读取一个简单的命令,并根据命令发送一个回复.问题是客户端有时可能对答案没兴趣并且提前退出,因此写入该套接字将导致SIGPIPE并使我的服务器崩溃.什么是防止崩溃的最佳做法?有没有办法检查线的另一边是否还在读?(select()似乎在这里不起作用,因为它总是说套接字是可写的).或者我应该用处理程序捕获SIGPIPE并忽略它?
我有一个非常简单的Python 3脚本:
f1 = open('a.txt', 'r')
print(f1.readlines())
f2 = open('b.txt', 'r')
print(f2.readlines())
f3 = open('c.txt', 'r')
print(f3.readlines())
f4 = open('d.txt', 'r')
print(f4.readlines())
f1.close()
f2.close()
f3.close()
f4.close()
Run Code Online (Sandbox Code Playgroud)
但它总是说:
IOError: [Errno 32] Broken pipe
Run Code Online (Sandbox Code Playgroud)
我在互联网上看到了解决这个问题的所有复杂方法,但是我直接复制了这个代码,所以我认为代码有问题而不是Python的SIGPIPE.
我正在重定向输出,所以如果上面的脚本命名为"open.py",那么我的运行命令是:
open.py | othercommand
Run Code Online (Sandbox Code Playgroud) 我写了一个基于posix套接字的客户端程序.该程序创建多个线程,并将锁定服务器.但是在gdb时间调试期间,程序会给出一个信息(错误)
Run Code Online (Sandbox Code Playgroud)(gdb) n Program received signal SIGPIPE, Broken pipe. [Switching to Thread 0xb74c0b40 (LWP 4864)] 0xb7fdd424 in __kernel_vsyscall () (gdb)
这是代码:
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
int get_hostname_by_ip(char* h , char* ip)
{
struct hostent *he;
struct in_addr **addr_list;
int i;
if ((he = gethostbyname(h)) == NULL)
{
perror("gethostbyname");
return 1;
}
addr_list = (struct in_addr **) he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++) …
Run Code Online (Sandbox Code Playgroud) 该程序SIGPIPE
在head -n 1
随机时间之后将其管理到" "之后生成.据我所知,因为我们在head -n 1
第一行之后给"更多" ,我们会期望它生成SIGPIPE
,但是在退出之前它会变成一个随机数(通常> 20和<200).知道为什么吗?
#include <stdio.h>
#include <stdlib.h>
main()
{
int i;
char *s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
i = 0;
while (1) {
fputs(s, stdout);
fflush(stdout);
fprintf(stderr, "Iteration %d done\n", i);
i++;
}
}
Run Code Online (Sandbox Code Playgroud)
这不是家庭作业,只是我教授的笔记中我不明白的东西.
如果在管道的任何步骤中存在非零错误的情况下捕获到非零错误,则设置bash pipefail
选项(via set -o pipefail
)允许脚本失败.
但是,我们SIGPIPE
遇到了错误(错误代码141),其中数据被写入不再存在的管道.
有没有办法设置bash来忽略SIGPIPE
错误,或者是否有一种方法来编写一个错误处理程序来处理所有错误状态代码,比如0和141?
例如,在Python中,我们可以添加:
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
Run Code Online (Sandbox Code Playgroud)
将默认行为应用于SIGPIPE
错误:忽略它们(参见http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-06/3823.html).
bash中是否有类似的选项?
我正在更新一个旧脚本,并遇到了一个我不熟悉的模式:
# NOTE The | : always returns true so the <cmd> doesn't fail
<cmd> | :
Run Code Online (Sandbox Code Playgroud)
我只见过用于叉炸弹示例的这种模式.如果有人问我如何完成评论中所述的内容,我会建议:
<cmd> ||:
Run Code Online (Sandbox Code Playgroud)
在我把这个打成一个错字之前,有没有人看过这种模式并且能够解释它的用例?
有一些与此问题相关的问题,但实际上没有一个问题能帮助我了解正在发生的事情。
完整的错误:
SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected) on request
Run Code Online (Sandbox Code Playgroud) 我SIGPIPE
在Xcode项目中遇到错误.自一周前开始显示此错误.如果我评论这个方法调用:[self configureBump];
一切正常.我在我的项目中集成了BUMP API.这个API工作到一周之前没有任何问题.我不确定这个错误的原因.有谁可以帮我解决这个错误?我的一些朋友也报告了这个错误.
Xcode版本:4.5 iOS版本:iOS 6.0/iOS 5.0
请参阅下面的堆栈跟踪:
* thread #1: tid = 0x1c03, 0x95a887d2 libsystem_kernel.dylib`mach_msg_trap + 10, stop reason = signal SIGPIPE
frame #0: 0x95a887d2 libsystem_kernel.dylib`mach_msg_trap + 10
frame #1: 0x95a87cb0 libsystem_kernel.dylib`mach_msg + 68
frame #2: 0x029ef13a CoreFoundation`__CFRunLoopServiceMachPort + 186
frame #3: 0x02952580 CoreFoundation`__CFRunLoopRun + 1312
frame #4: 0x02951db4 CoreFoundation`CFRunLoopRunSpecific + 212
frame #5: 0x02951ccb CoreFoundation`CFRunLoopRunInMode + 123
frame #6: 0x03093879 GraphicsServices`GSEventRunModal + 207
frame #7: 0x0309393e GraphicsServices`GSEventRun + 114
frame #8: 0x017a0a9b …
Run Code Online (Sandbox Code Playgroud) 我正在使用带有 python 子处理命令的 python 包。当我并行 qsub 多个作业时,它显示错误: subprocess.CalledProcessError: die with
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/anaconda3/envs/lib/python3.6/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/anaconda3/envs/syniu/lib/python3.6/subprocess.py", line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['bismark', '/fasta', '--bowtie2', 'R1.trimed.fq.gz', '--pbat', '-o', '/M1-1-2-A3', '--temp_dir', '/M1-1-2-A3']' died with <Signals.SIGPIPE: 13>.
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/anaconda3/envs/bin/yap", line 11, in <module>
sys.exit(main())
File "/anaconda3/envs/lib/python3.6/site-packages/__main__.py", line 970, in …
Run Code Online (Sandbox Code Playgroud) 我试图忽略我在 Swift 应用程序中使用的第三方 SDK 抛出的 SIGPIPE 信号。如何让我的应用程序全局忽略 SIGPIPE 信号?
sigpipe ×10
c ×3
signals ×3
bash ×2
python ×2
broken-pipe ×1
bump ×1
colon ×1
gdb ×1
io ×1
ios6 ×1
ios8 ×1
objective-c ×1
pipe ×1
pthreads ×1
python-3.6 ×1
python-3.x ×1
sockets ×1
subprocess ×1
swift ×1
uwsgi ×1