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:
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
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:

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

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


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

mysql> select repeat('a',@@global.read_buffer_size) into outfile '\\\\\\c\\yay.txt';
ERROR 3 (HY000): Error writing file '\\\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
[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 */

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.