From 162eadbe68d6029522b23ab57c84a0b64f6699a9 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 3 Mar 2008 17:34:06 +0300 Subject: [PATCH] Fix for bug #31781: multi-table UPDATE with temp-pool enabled fails with errno 17 my_create() did not perform any checks for the case when a file is successfully created by a call to open(), but the call to my_register_filename() later fails because the number of open files has exceeded the my_open_files limit. This can happen on platforms which do not have getrlimit(), and hence we do not know the real limit for open files. In such a case an error was returned to a caller although the file has actually been created. Since callers assume my_create() to return an error only when it failed to create a file, they did not perform any cleanups, leaving an 'orphaned' file on the file system. Fixed by adding a check for the above case to my_create() and ensuring the newly created file is deleted before returning an error. Creating a deterministic test case in the test suite is impossible, because the exact steps required to reproduce the above situation depend on the platform and/or environment (OS per-user limits, queries executed by previous tests, startup parameters). The patch was manually tested on Windows using examples posted in the bug report. mysys/my_create.c: Ensure that, if the call to my_register_filename() in my_create() failed, but the previous open() called succeeded, the newly created file is deleted before returning an error. --- mysys/my_create.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/mysys/my_create.c b/mysys/my_create.c index d612926c1a5..c535ae73a0a 100644 --- a/mysys/my_create.c +++ b/mysys/my_create.c @@ -35,7 +35,7 @@ File my_create(const char *FileName, int CreateFlags, int access_flags, myf MyFlags) { - int fd; + int fd, rc; DBUG_ENTER("my_create"); DBUG_PRINT("my",("Name: '%s' CreateFlags: %d AccessFlags: %d MyFlags: %d", FileName, CreateFlags, access_flags, MyFlags)); @@ -60,6 +60,20 @@ File my_create(const char *FileName, int CreateFlags, int access_flags, fd = open(FileName, access_flags); #endif - DBUG_RETURN(my_register_filename(fd, FileName, FILE_BY_CREATE, - EE_CANTCREATEFILE, MyFlags)); + rc= my_register_filename(fd, FileName, FILE_BY_CREATE, + EE_CANTCREATEFILE, MyFlags); + /* + my_register_filename() may fail on some platforms even if the call to + *open() above succeeds. In this case, don't leave the stale file because + callers assume the file to not exist if my_create() fails, so they don't + do any cleanups. + */ + if (unlikely(fd >= 0 && rc < 0)) + { + int tmp= my_errno; + my_delete(FileName, MyFlags); + my_errno= tmp; + } + + DBUG_RETURN(rc); } /* my_create */