From 8e7b22cf1b2c2be90b7a9b2fe1594ed2658648f1 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Tue, 6 Feb 2018 12:02:23 -0800 Subject: [PATCH] Close input files that were being left open after parallel reading --- main.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/main.cpp b/main.cpp index 152b0e6..50de5e1 100644 --- a/main.cpp +++ b/main.cpp @@ -1198,6 +1198,16 @@ int read_input(std::vector &sources, char *fname, int maxzoom, int minzo double dist_sum = 0; size_t dist_count = 0; + int files_open_before_reading = open("/dev/null", O_RDONLY | O_CLOEXEC); + if (files_open_before_reading < 0) { + perror("open /dev/null"); + exit(EXIT_FAILURE); + } + if (close(files_open_before_reading) != 0) { + perror("close"); + exit(EXIT_FAILURE); + } + size_t nsources = sources.size(); for (size_t source = 0; source < nsources; source++) { std::string reading; @@ -1383,6 +1393,11 @@ int read_input(std::vector &sources, char *fname, int maxzoom, int minzo perror("munmap source file"); exit(EXIT_FAILURE); } + + if (close(fd) != 0) { + perror("close input file"); + exit(EXIT_FAILURE); + } } else { FILE *fp = fdopen(fd, "r"); if (fp == NULL) { @@ -1543,6 +1558,22 @@ int read_input(std::vector &sources, char *fname, int maxzoom, int minzo } } + int files_open_after_reading = open("/dev/null", O_RDONLY | O_CLOEXEC); + if (files_open_after_reading < 0) { + perror("open /dev/null"); + exit(EXIT_FAILURE); + } + if (close(files_open_after_reading) != 0) { + perror("close"); + exit(EXIT_FAILURE); + } + + if (files_open_after_reading > files_open_before_reading) { + fprintf(stderr, "Internal error: Files left open after reading input. (%d vs %d)\n", + files_open_before_reading, files_open_after_reading); + ret = EXIT_FAILURE; + } + if (!quiet) { fprintf(stderr, " \r"); // (stderr, "Read 10000.00 million features\r", *progress_seq / 1000000.0);