Bug #119304 SQL_LOAD-xxxx file exists when the SQL thread checks permissions, causing the SQL thread fail to start
Submitted: 5 Nov 2:08 Modified: 5 Nov 2:12
Reporter: yl yl Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:5.7,8.0 OS:Any
Assigned to: CPU Architecture:Any

[5 Nov 2:08] yl yl
Description:
In check_temp_dir(), mysqld creates a temporary file (SQL_LOAD-uuid) to check permission to create a file, with flag O_EXCL.

--------
  snprintf(unique_tmp_file_name, size_of_tmp_file_name, "%s%s%s", tmp_file,
           channel_name, server_uuid);
  if ((fd = mysql_file_create(key_file_misc, unique_tmp_file_name, CREATE_MODE,
                              O_WRONLY | O_EXCL | O_NOFOLLOW, MYF(MY_WME))) < 0)
    return 1;

  /*
    Clean up.
   */
  mysql_file_close(fd, MYF(0));

  mysql_file_delete(key_file_misc, unique_tmp_file_name, MYF(0));
  my_free(unique_tmp_file_name);
  return 0;

and in my_create(), with flag O_CREAT
----------
  fd = mysys_priv::RetryOnEintr(
      [&]() {
        return open(FileName, AccessFlags | O_CREAT,
                    CreateFlags ? CreateFlags : my_umask);
      },
      -1);

In POSIX Programmer's Manual, if the file exists, this flag has no effect except as noted under O_EXCL below.
So if the SQL_LOAD-xxx file exists, the SQL thread  will report the following error and unable to start again:
Unable to use slave's temporary directory /tmp - Can't create/write to file '/tmp/SQL_LOAD-5a86a661-b7b1-11f0-bc6d-0242ac11000c' (OS errno 17 - File exist)

How to repeat:
stop slave;
create the file SQL_LOAD-uuid in replica_load_tmpdir.
start slave;

Suggested fix:
remove O_EXCL flag when mysql_file_create in check_temp_dir
or
Check to see if the file exists, and delete it before create.
[5 Nov 2:12] yl yl
If O_CREAT and O_EXCL are set, open() shall fail if the file exists.