nodejs写入文件的一个小bug
2013-03-11 11:40
302 查看
使用fs的appendFile写日志,线上某些机器报
查看报错行内容是 fs.appendFile(file , logTxt + "\n") ,看手册上这个调用方法为
万能google给了答案 》》https://github.com/joyent/node/issues/4352
This only happens if you choose not to pass in a callback to the method. This is because an EMFILE error occurs, since there are too many open file handles, and the code wishes to pass an error to the user, however the documentation does indicate that the callback should be optional.
看来这是个bug,要它不报错倒是简单, 传个空函数 function(){} 作为回调函数就好了
PS
PS2 文件句柄过段时间会自己回收掉 如果调用不是很频繁的话 就不会触发这个报错 ,线上是上了一个访问量很大的页面才暴露了这个问题
PS3 任何IO操作都可能导致EMFILE这个异常
比如打开多个socket端口
TypeError: object is not a function
查看报错行内容是 fs.appendFile(file , logTxt + "\n") ,看手册上这个调用方法为
fs.appendFile(filename, data, encoding='utf8', [callback])
貌似调用没什么问题,而且之前代码也运行正常万能google给了答案 》》https://github.com/joyent/node/issues/4352
Repeated calls to fs.appendFile without callback will cause an error
当调用fs 又没设置回调函数的时候会报错This only happens if you choose not to pass in a callback to the method. This is because an EMFILE error occurs, since there are too many open file handles, and the code wishes to pass an error to the user, however the documentation does indicate that the callback should be optional.
看来这是个bug,要它不报错倒是简单, 传个空函数 function(){} 作为回调函数就好了
PS
Error: EMFILE, too many open files 可以通过设置系统参数修改
sysctl -w kern.maxfiles=20480
PS2 文件句柄过段时间会自己回收掉 如果调用不是很频繁的话 就不会触发这个报错 ,线上是上了一个访问量很大的页面才暴露了这个问题
PS3 任何IO操作都可能导致EMFILE这个异常
比如打开多个socket端口
var count =0;for(var i =0; i<1000;i++){var ss =require('net').Socket({fd:null,type:'tcp4',allowHalfOpen:false}); ss.connect(1234,function(){ count++; console.log(count);});}
相关文章推荐
- Linux常用命令:文件解压缩、去重、合并为一个文件、一个文件的指定行写入另一个文件(不断更新中...)
- Oracle 11.2.0.1的又一个隐藏在ORA-03113后的bug: 通信通道的文件结尾
- php将数据写入另外一个文件
- 快速的将一个结构体写入文件--内存映射文件
- python (1)一个简单的爬虫: python 在windows下 创建文件夹并写入文件
- JavaSE8基础 SequenceInputStream 将多个txt文件中的内容 写入到一个txt文件中
- tee可以看见输出并将其写入到一个文件中
- Xcode4.6.1上的一个bug-本地化/国际化Xib文件无效
- 如何从内核模式设备驱动程序中打开一个文件以及如何读取或写入文件(来自微软官网)
- 在Excel(xlsx)文件中用OpenXml SDK 添加一个新的Worksheet并写入字符串
- nodejs分析excel文件将结果写入excel文件
- 创建一个文件,并从键盘写入数据
- [原创]发现QQ截图功能的一个奇怪bug(不能选择保存文件类型)
- 在nodejs中如何用异步的方式读取一个文件
- 读取一个文件写入另一个文件
- 将文件夹下的各文件内容写入一个文件中
- 随机写入一个文件一些字符
- java读写文件-经过多方搜索整合成我需要的功能(读写指定文件夹下面的多个指定名称的txt中的内容全部写入到一个指定的txt中)
- 从键盘输入两个学生数据,写入一个文件中, 再读出这两个学生的数据显示在屏幕上。
- [Bug]CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temp