Bug #55662 _dosmaperr wipes out the errno set in _open_osfhandle and gives (errno: 22)
Submitted: 31 Jul 2010 14:00 Modified: 6 Nov 2012 3:29
Reporter: Shane Bester (Platinum Quality Contributor) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Windows Severity:S3 (Non-critical)
Version:5.1.49 OS:Windows
Assigned to: CPU Architecture:Any

[31 Jul 2010 14:00] Shane Bester
Description:
When some posix call failed on windows, we get some error like this;
ERROR 3 (HY000): Error writing file ... (Errcode: 22)

The above message can be considered useful or useless, depending on the problem.
Inspecting the _errno function in C:\Program Files (x86)\Microsoft Visual Studio 8\VC\crt\src\dosmap.c shows us more information is available:

<cut>
    _ptiddata ptd = _getptd_noexit();
    if (!ptd) {
        return &ErrnoNoMem;
    } else {
        return ( &ptd->_terrno );
    }
</cut>

Specifically, I want to see the value of pdt->_tdoserrno which is 0x5AA
in this case, which is the underlying error.

ERROR_NO_SYSTEM_RESOURCES 1450

How to repeat:
If you cannot tell me the cause of this error message:

mysql> select repeat('a',@@global.read_buffer_size) into outfile '\\\\192.168.1.102\\c\\yay.txt';
ERROR 3 (HY000): Error writing file '\\192.168.1.102\c\yay.txt' (Errcode: 22)

Then you've repeated the feature request, which is more information required in the error message, or error log.
[30 Sep 2010 14:39] MySQL Verification Team
http://msdn.microsoft.com/en-us/library/t3ayayh1(VS.80).aspx
[30 Sep 2010 15:33] MySQL Verification Team
There is bug here, for example, in my_sopen():

  if ((fh= _open_osfhandle((intptr_t)osfh, 
                           oflag & (_O_APPEND | _O_RDONLY | _O_TEXT))) == -1)
  {
    _dosmaperr(GetLastError());     /* map error */
    CloseHandle(osfh);
  }

When _open_osfhandle() fails, it sets errno to EMFILE (24).
I'd suggest either not calling _dosmaperr, or first saving errno...
The next call to _dosmaperr(GetLastError()); sets errno to EINVAL (22)
since the OS error was zero.

As a result, we have different code paths - We get:
ERROR 1017 (HY000) at line 4485: Can't find file: '.\test\t1099.frm' (errno: 22)
Instead of the expected:
ERROR 1016 (HY000) at line 4485: Can't open file: '.\test\t1099.frm' (errno: 24)

Since mysql takes a different code path in the case of errno 24, this is a bug.
[6 Nov 2012 3:29] Philip Olson
This bug is fixed in the 5.7 branch, and the changelog entry reads as follows:

  On Microsoft Windows, a failed API or function call in
  "mysqld.exe" could sometimes report the error code 22, 
  instead of the proper error code.

Thank you for the bug report.