[Mono-dev] file handle leak in cpu_load

Markus Beth markus.beth at zkrd.de
Tue Dec 2 11:01:29 UTC 2014


Hi all,

last week we used the profiler of a mono 3.10.0 installation on 
Linux x86_64 and we found it leaks file handles (for /proc/loadavg).

I located the problem in the function cpu_load in 
mono/utils/mono-counters.c. If a premature return is taken, the FILE 
pointer f is not fclose()-ed.

I propose the following fix:

---
  mono/utils/mono-counters.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mono/utils/mono-counters.c b/mono/utils/mono-counters.c
index 1e0840c..c669454 100644
--- a/mono/utils/mono-counters.c
+++ b/mono/utils/mono-counters.c
@@ -360,6 +360,7 @@ cpu_load (int kind)
         FILE *f = fopen ("/proc/loadavg", "r");
         if (f) {
                 len = fread (buffer, 1, sizeof (buffer) - 1, f);
+               fclose (f);
                 if (len > 0) {
                         buffer [len < 511 ? len : 511] = 0;
                         b = buffer;
@@ -374,7 +375,6 @@ cpu_load (int kind)
                                 }
                         }
                 }
-               fclose (f);
         }
  #endif
         return 0;
-- 

It applies on master and also (with an offset of -32 lines) on 
mono-3.10.0-branch.

What do you think?

Regards,
Markus


More information about the Mono-devel-list mailing list