五花八门客户问题(BUG) - 重复打印

根据commit猜测客户问题

git://sourceware.org / glibc.git / commit

commit

summary | shortlog | log | commit | commitdiff | tree
(parent: 4573c6b) | patch
Don't flush write buffer for ftell
author	Siddhesh Poyarekar <siddhesh@redhat.com>	
Fri, 28 Sep 2012 13:07:23 +0000 (18:37 +0530)
committer	Siddhesh Poyarekar <siddhesh@redhat.com>	
Fri, 28 Sep 2012 13:08:14 +0000 (18:38 +0530)
commit	adb26faefe47b7d34c941cbfc193ca7a5fde8e3f
tree	799a1e10dae3c9aac2b9c2c79e0b8adaaa1eae90	tree
parent	4573c6b09884a93fffa3a754678ef881cadebfb3	commit | diff
Don't flush write buffer for ftell

真实客户问题

客户发现打印到文件的内容重复了。这不是普通文件,我们是做数据库的,影响很大。

问题所在

大喜的是问题很好重现,所以直接debug吧。在所有的fwrite函数上下断点,愣是没看出来怎么重复的。后来才怀疑到fwrite+fork这项操作上。如下是一个简易版的重现代码。踩踩结果吧:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

void main(){
    FILE* file=fopen("test.txt", "a+");
    fwrite("First", 1, 5, file);
    fwrite("Secon", 1, 5, file);
    ftell(file); /*NOTE: redhat 6/AIX7  flushs the data, while centos 7 doesn't flush!!!!*/
    //setbuf(file, 0);
    if(fork()>0){
           fwrite("In Parent", 1, 9, file);
           exit(0);
     }else{
           fclose(file);
     }
}

结果:FirstSeconIn ParentFirstSecon

相信读者应该已经注意到上面的注释了,与glibc中的ftell有关系,redhat6中ftell不会强刷缓冲中的数据到磁盘,但是redhat7却会,这是文章一开始的glibc的commit导致的。基础软件的一点点更新都是影响巨大啊。

fix办法

fix也很简单,只需要在fork之前调用下fflush即可。

相关推荐

  1. 五花八门客户问题(BUG) - 重复打印

    2023-12-06 17:16:05       46 阅读
  2. 五花八门客户问题(BUG) - 数据库索引损坏

    2023-12-06 17:16:05       46 阅读
  3. 五花八门客户问题(BUG) - 用好strace

    2023-12-06 17:16:05       38 阅读
  4. 五花八门客户问题(BUG) - 用好strace2

    2023-12-06 17:16:05       41 阅读
  5. Webpack5 解决静态资源重复打包问题

    2023-12-06 17:16:05       6 阅读
  6. bug清单问题

    2023-12-06 17:16:05       6 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-06 17:16:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-06 17:16:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-06 17:16:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-06 17:16:05       18 阅读

热门阅读

  1. linux下查看硬盘哪些文件夹比较大

    2023-12-06 17:16:05       31 阅读
  2. 什么是Redis?

    2023-12-06 17:16:05       42 阅读
  3. 开发一款短剧视频小程序软件多少钱?

    2023-12-06 17:16:05       34 阅读
  4. 什么是Ros(一)- 名词解释和架构概述

    2023-12-06 17:16:05       40 阅读
  5. Ubuntu 20.4 源代码方式安装 cdo(笔记)

    2023-12-06 17:16:05       31 阅读
  6. 入门 PyTorch

    2023-12-06 17:16:05       39 阅读