避免SIGPIPE导致的iOS应用闪退/Avoiding SIGPIPE signal crash in iOS(mach_msg_trap、SIGPIPE信号)

问题描述:

应用运行时,锁屏后再打开有一定几率闪退。通过真机调试发现程序会中断在此处:

libsystem_kernel.dylib`mach_msg_trap:

解决思路:

  • 通过这篇文章了解是进程收到 SIGPIPE  信号,该信号默认行为是终止进程。

The process received a SIGPIPE . The default behaviour for this signal is to end the process.

A SIGPIPE is sent to a process if it tried to write to a socket that had been shutdown for writing or isn‘t connected (anymore).

To avoid that the program ends in this case, you could either

  • make the process ignore SIGPIPE or
  • install an explicit handler for SIGPIPE (typically doing nothing).

In both cases send*()/ write() would return -1 and set errno to EPIPE.

SIGPIPE相关信息

当连接关闭后,进程默认会收到 SIGPIPE 信号。如果这个信号没有被处理或者忽略,程序就会立刻退出。

文档中给出两套解决方案:

When a connection closes, by default, your process receives a SIGPIPE  signal. If your program does not handle or ignore this signal, your program will quit immediately. You can handle this in one of two ways:

  • Ignore the signal globally with the following line of code:
signal(SIGPIPE, SIG_IGN);
  • Tell the socket not to send the signal in the first place with the following lines of code (substituting the variable containing your socket in place of sock):
int value = 1;
setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value));

For maximum compatibility, you should set this flag on each incoming socket immediately after calling accept in addition to setting the flag on the listening socket itself.

Avoiding Common Networking Mistakes中内容(源自APPLE)

  • 在著名的栈溢出stackoverflow网站中找到第三种解决方案(因Mac OS中未定义MSG_NOSIGNAL故无法测试):

If you‘re using the send() call, another option is to use the MSG_NOSIGNAL option, which will turn the SIGPIPE behavior off on a per call basis. Note that not all operating systems support the MSG_NOSIGNAL flag.

参考链接:

1.iOS异常处理:mach_msg_trap处异常 - http://www.jianshu.com/p/2b3f58c61d7d

2.如何在 iOS 上避免 SIGPIPE 信号导致的 crash (Avoiding SIGPIPE signal crash in iOS) - http://www.jianshu.com/p/1957d2b18d2c

3.Program received signal SIGPIPE, Broken pipe.? - https://stackoverflow.com/questions/18935446/program-received-signal-sigpipe-broken-pipe

4.How to prevent SIGPIPEs (or handle them properly) - https://stackoverflow.com/questions/108183/how-to-prevent-sigpipes-or-handle-them-properly

5.Avoiding Common Networking Mistakes - https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/CommonPitfalls/CommonPitfalls.html

时间: 2024-08-11 17:38:31

避免SIGPIPE导致的iOS应用闪退/Avoiding SIGPIPE signal crash in iOS(mach_msg_trap、SIGPIPE信号)的相关文章

iOS开发-闪退问题-解决之前上架的 App 在 iOS 9 会闪退问题

最新更新:(2015.10.02) 开发环境: Delphi 10 Seattle OS X El Capitan v10.11 需使用下列 HotfixID: 30398, PAServer Hotfix for Delphi, C++Builder and RAD Studio 10 Seattle Xcode v7.0.1 iOS SDK v9.0 真机测试(以下机种皆不闪退): iPhone 3GS v6.1.2 (32 bit) iPhone 4 v7.1.2 (32 bit) iPh

iOS 启动连续闪退保护方案

引言 “如果某个实体表现出以下任何一种特性,它就具备自主性:自我修复.自我保护.自我维护.对目标的自我控制.自我改进.” —— 凯文·凯利 iOS App 有时可能遇到启动必 crash 的绝境:每次打开 App 都闪退,无法正常使用App. 为了尝试解决这个问题,微信读书开发了 iOS 连续闪退保护工具:GYBootingProtection,检测连续闪退,在连续闪退出现时,尝试自修复 App: 本文探讨了连续闪退问题的产生原因.检测.修复机制,以及如何在你的项目中引入.测试和使用 GYBoo

解决之前上架的 App 在 iOS 9 会闪退问题 (更新:已有 Hotfix)

解决之前上架的 App 在 iOS 9 会闪退问题 (更新:已有 Hotfix) 最新更新:(2015.10.02) 开发环境: Delphi 10 Seattle OS X El Capitan v10.11 需使用下列 HotfixID: 30398, PAServer Hotfix for Delphi, C++Builder and RAD Studio 10 Seattle Xcode v7.0.1 iOS SDK v9.0 真机测试(以下机种皆不闪退): iPhone 3GS v6.

iOS-程序崩溃与闪退原因

1.iOS-中app启动闪退的原因 2.iOS开发-闪退问题-解决之前上架的 App 在 iOS 9 会闪退问题 3.iOS-应用闪退总结 4.iOS开发-捕获程序崩溃日志 5.iOS开发-应用崩溃日志揭秘(一) 6.iOS开发--应用崩溃日志揭秘(二)

tomcat启动不成功(点击startup.bat闪退)的解决办法

今天遇到tomcat手动启动不成功,但是再eclipse里面是可以启动成功的.我一直开始以为是端口被占用了,最后才发现是这么一回事.首先说下我的tomcat是安装版的.免安装的tomcat双击startup.bat后,启动窗口一闪而过,而且tomcat服务未启动.这个原因就是:在启动tomcat是,需要读取环境变量和配置信息,缺少了这些信息,就不能登记环境变量,导致了tomcat的闪退. 解决办法: 1:在已解压的tomcat的bin文件夹下找到startup.bat,右击->编辑.在文件头加入

APP闪退问题

1.iOS-中app启动闪退的原因 2.iOS开发-闪退问题-解决之前上架的 App 在 iOS 9 会闪退问题 3.iOS-应用闪退总结 4.iOS开发-捕获程序崩溃日志 5.iOS开发-应用崩溃日志揭秘(一) 6.iOS开发--应用崩溃日志揭秘(二) 7.iOS-----Crash文件分析(一) 8.iOS-----dSYM 文件分析工具配合Xcode符号化 crash日志 9.iOS------苹果设备处理器指令集(iPhone初代到iPhone5s)规避偏僻的闪退原因,打包iPa包时正确

直接双击启动tomcat中的startup.bat闪退原因及解决方法

免安装的tomcat双击startup.bat后,启动窗口一闪而过,而且tomcat服务未启动. 原因是:在启动tomcat是,需要读取环境变量和配置信息,缺少了这些信息,就不能登记环境变量,导致了tomcat的闪退. 解决办法: 1.在已解压的tomcat的bin文件夹下找到startup.bat,右击->编辑.在文件头加入下面两行: SET JAVA_HOME=D:\Java\jdk1.6.0_10 (java jdk目录) SET TOMCAT_HOME=E:\tomcat-6.0.35

Windows下运行Tomcat闪退问题

直接双击startup.bat后闪退,可能的原因是tomcat的配置没有完全正确 完整的tomcat的配置应该配置: 1.JAVA_HOME 2.CATALINA_HOME 3.Path:%CATALINA_HOME%\lib;%CATALINA_HOME%\bin 我就是没有配置path,导致运行tomcat一直闪退,在cmd中到startup.bat的目录中执行,提示需要jre_home,其实并不是jre_home的问题,jre_home 并不用配置,只需要配置JAVA_HOME就行,下面是

启动tomcat闪退

启动tomcat,点击startup.bat闪退 使用的Tomcat是免安装版本的. 因为在启动tomcat是需要读取环境变量和配置信息,缺少了这些信息,就不能登记环境变量,导致了tomcat的闪退. 解决办法: 1:在已解压的tomcat的bin文件夹下找到startup.bat,右击->编辑.在文件头加入下面两行: SET JAVA_HOME=D:\Java\jdk1.7 (java jdk目录)SET TOMCAT_HOME=E:\tomcat-7.0 (解压后的tomcat文件目录) 2