File Descriptor leakage causes Crash: Too Temporary open files, descriptorcrash

Source: Internet
Author: User

[Convert] Crash: Too Temporary open files, descriptorcrash caused by File Descriptor Leakage

In the actual Android development process, we encountered some strange Crash. Using sigaction in combination with libcorkscrew and some third-party Crash Reporter did not capture the specific information about Crash. Then, we can use the CallStack of Java reported by buugly to observe that these Crash found some common information:

It seems to be related to OpenGL, So we further observe the log output by the program and find that:

The following information is obtained from the log:

We have tried our existing devices repeatedly. It is true that only Adreno devices (such as Xiaomi 3, HTC M8, and Huawei P7) will encounter such strange random Crash under specific conditions. Other devices, such as Xiaomi Pad (Tegra) and Samsung S3 (Mali), do not have this problem. This problem is indeed a headache. I have searched the internet for a long time and have not found any useful information. When testing again On Xiaomi 3, we found that there is an inevitable message in the log:

E/MemoryHeapBase(18703): error creating ashmem region: Too many open files

This information indirectly pointed out the problem and gave us some tips: It seems that too many files have been opened. With this aura, we try to output all opened files in the program:

 SHOW FILE HANDLES: 0  (socket:[285038]): read-write 1  (/dev/null): read-write 2  (/dev/null): read-write 3  (/dev/log/main): cloexec write-only 4  (/dev/log/radio): cloexec write-only 5  (/dev/log/events): cloexec write-only 6  (/dev/log/system): cloexec write-only 7  (/sys/kernel/debug/tracing/trace_marker): write-only 8  (/dev/__properties__): 9  (/dev/binder): cloexec read-write10  (/dev/log/main): cloexec write-only11  (/dev/log/radio): cloexec write-only12  (/dev/log/events): cloexec write-only13  (/dev/log/system): cloexec write-only14  (/system/framework/framework-res.apk):15  (/system/framework/core-libart.jar):16  (pipe:[282578]): nonblock17  (/dev/alarm):18  (/dev/cpuctl/tasks): cloexec write-only19  (/dev/cpuctl/bg_non_interactive/tasks): cloexec write-only20  (socket:[282569]): read-write21  (pipe:[282570]):22  (pipe:[282570]): write-only23  (pipe:[282578]): nonblock write-only24  (anon_inode:[eventpoll]): read-write25  (/data/app/---app_name---/base.apk):26  (/data/data/---app_name---/databases/bugly_db): cloexec read-write27  (socket:[285047]): read-write28  (anon_inode:mali-8938): cloexec29  (socket:[282605]): nonblock read-write30  (socket:[283605]): nonblock read-write31  (/dev/null): read-write32  (/dev/ump): read-write33  (socket:[285045]): nonblock read-write34  (/dev/null): read-write35  (/dev/mali): read-write36  (anon_inode:mali-8938): cloexec37  (anon_inode:mali-8938): cloexec38  (/data/app/---app_name---/base.apk):39  (anon_inode:mali-8938): cloexec40  (anon_inode:mali-8938): cloexec41  (/dev/null): read-write42  (/dev/null): read-write43  (/data/app/---app_name---/base.apk):44  (/dev/null): read-write45  (anon_inode:mali-8938): cloexec46  (/data/data/---app_name---/files/DefaultFont.ttf):47  (/data/app/---app_name---/base.apk):48  (anon_inode:sync_fence):49  (/dev/null): read-write50  (socket:[285060]): cloexec read-write52  (anon_inode:mali-8938): cloexec53  (anon_inode:mali-8938): cloexec54  (/dev/null): read-write55  (anon_inode:sync_fence):56  (pipe:[284134]): write-only58  (anon_inode:sync_fence):62  (anon_inode:sync_fence):63  (anon_inode:sync_fence):

Through continuous testing, we found that the number of opened files has been increasing, and when the number of opened files is close to 1024, the above eglSwapBuffers will inevitably fail. So we come to an intermediate conclusion:

  • If the program opens too many files, it will cause OpenGL swap buffer to fail!

This seems to be a little nonsense, because the two do not have any connection. This problem only occurs on Adreno's GPU, So we guess:

  • The Adreno driver needs to apply for a new FD when using the swap buffer. This FD may be some hardware IO, which is unknown;
  • If the usage of various FD in the program is too close to the upper limit, the Adreno driver cannot apply for the necessary FD, resulting in swap buffer failure.

This seems to make sense. Although sawp buffer itself does not Crash, it does not raise any signal, but simply returns an error, but this causes an exception in the upper-layer logic. These exceptions are different on different devices:

  • Some devices will trigger exceptions on the Java layer in eglSwapBuffers, causing Crash;
  • Some devices do not encounter exceptions, but OpenGL stops working (halt rendering). The result is that the program gets stuck and no response is returned;
  • Some devices may not do anything, but if your interaction triggers other logic: for example, press the rollback key to bring up a dialog box, the dialog box also requires FD, but cannot be obtained, the logic in the pop-up dialog box throws an exception,

So there are various strange Crash.

Solution

Through code troubleshooting, we found that FD leakage exists when using SoundPool to process sound effects:

1  private SoundPool m_soundPool;2  public int loadSound(String path) {3      int soundID = m_soundPool.load(getAssets().openFd(path), 0);4      return soundID;5  }6  public unloadSound(int soundID) {7      m_soundPool.unload(soundID);8  }

Although we unmount these sound effects when we do not need them, we do not know whether it is a defect of the SoundPool class or whether we use them improperly, in actual tests, we found that after unload, the FD opened through openFd in load is not released. Force call System. gc () can release this part of FD on some devices (such as Xiaomi 3), but other devices (such as HTC M8) Cannot uninstall gc even if it is forced, therefore, FD leakage occurs.

In the end, we manage these FD manually and manually call the close method of these FD during unload:

 1  private SoundPool m_soundPool; 2  private HashMap<Integer, AssetFileDescriptor> m_soundFdMap 3  public int loadSound(String path) { 4      AssetFileDescriptor fd = getAssets().openFd(path); 5      int soundID = m_soundPool.load(fd, 0); 6      m_soundFdMap.put(soundID, fd); 7      return soundID; 8  } 9  public unloadSound(int soundID) {10     m_soundPool.unload(soundID);11     m_soundFdMap.get(soundID).close();12  }

After that, FD was no longer leaked, and various strange Crash on various devices were processed.

Summary

This problem is roughly said: because too many sound effects are played, the underlying rendering of Adreno fails, so that the upper layer logic is various and produces many strange Crash. The accurate explanation should be: the FD leakage in the program is the same as the memory leakage. The exhaustion of FD will cause various exceptions of the program as if the memory is exhausted, however, the former is not as well-known as the latter, and the latter is easy to detect.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.