Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests/tr1: Fix sporadic failures caused by tmpnam/_tempnam #2210

Merged
merged 9 commits into from
Sep 25, 2021

Conversation

StephanTLavavej
Copy link
Member

While investigating failures that @mnatsuhara encountered in the internal test harness, @CaseyCarter encountered sporadic failures in the tr1 legacy test suite:

Click to expand test failure output from tests/tr1/tests/fstream1 and fstream2.
RUN.PM line 750:  Executing: "fstream1.exe".
FAIL test 061 at line 247 in .\test.cpp: nfs.is_open()
FAIL test 062 at line 248 in .\test.cpp: nfs.good()
GOT "" != "this is a test"
FAIL test 063 at line 253 in .\test.cpp: buf == "this is a test"
GOT "" != "this is only a test"
FAIL test 064 at line 255 in .\test.cpp: buf == "this is only a test"
GOT "" != "this is still just a test"
FAIL test 065 at line 257 in .\test.cpp: buf == "this is still just a test"
GOT "" != "one last test"
FAIL test 066 at line 259 in .\test.cpp: buf == "one last test"
GOT "" != "one more last test"
FAIL test 067 at line 261 in .\test.cpp: buf == "one more last test"
FINISHED testing <fstream>, part 1
RUN.PM line 750:  Executing: "fstream2.exe". 
GOT L"this is still just a test" != L"one more last test" 
FAIL test 063 at line 232 in .\test.cpp: buf == L"one more last test"

The problem appears to be that tr1 uses tmpnam and _tempnam to generate temporary file names. By default, these functions generate temporary file names in the user-global temp directory, and they're either short enough to be collision-prone, or sequential. 🙀 This is a problem when tests are run in parallel:

Click to expand example output from tmpnam and _tempnam, plus the new function being added in this PR.
C:\Temp>type meow.cpp
#include "temp_file_name.h"
#include <filesystem>
#include <stdio.h>

using namespace std;
using namespace std::filesystem;

int main() {
    printf("             tmpnam: %s\n", tmpnam(nullptr));
    printf("           _tempnam: %s\n", _tempnam(".", ""));
    printf("temp_directory_path: %s\n", temp_directory_path().string().c_str());
    printf("     temp_file_name: %s\n", temp_file_name().c_str());
}
C:\Temp>cl /EHsc /nologo /W4 /std:c++17 /D_CRT_SECURE_NO_WARNINGS /ID:\GitHub\STL\tests\tr1\include meow.cpp && meow
meow.cpp
             tmpnam: C:\Users\stl\AppData\Local\Temp\s890.0
           _tempnam: C:\Users\stl\AppData\Local\Temp\2
temp_directory_path: C:\Users\stl\AppData\Local\Temp\
     temp_file_name: temp_file_4C2A015E48E7C09E7CCC8EC2913CE7B60AC451786E8B4995F7B96E478B3B7DBA.tmp

@cbezault explained that the GitHub test harness works around this problem by setting the TMP environment variable to a unique directory for each configuration of each test:

shared.env['TMP'] = execDir
shared.env['TEMP'] = execDir
shared.env['TMPDIR'] = execDir
shared.env['TEMPDIR'] = execDir

We didn't realize this was a problem for the internal test harness because it had an overall high level of flakiness (which we're trying to improve now), and because it reruns failed tests up to 10 times before logging the failure.

I believe we should permanently avoid this by generating truly unique names in the local directory. I'm adding a new header to generate names of the form "temp_file_<64 hexits>.tmp" and replacing all of tr1's calls to tmpnam and _tempnam. (I checked std and tr1 for calls to the more modern temp_directory_path(), and none of those were problematic.)

Note that because tr1 is the legacy test suite, I am not attempting to refactor/cleanup nearby code, even though I see many things that could be improved/simplified. The only cleanups I'm performing are eliminating NO_TMPNAM_TESTS and tmpnam macroization, to make it easier to see what needs to be changed.

The new filenames that I'm generating are comfortably below the 100 and L_tmpnam (260) limits in the source code.

I checked to see if any additional changes were needed, and I don't believe so. tmpnam either writes into a provided buffer, or returns a pointer to an internal buffer (thus, replacing it with a pointer into a named string is always fine). _tempnam allocates memory with malloc that should be freed, and the tests were simply leaking this, so there are no free calls that need to be removed.

I chose to use std:: qualification in temp_file_name.h instead of including tr1's central headers and using its STD macro.

@StephanTLavavej StephanTLavavej added the test Related to test code label Sep 18, 2021
@StephanTLavavej StephanTLavavej requested a review from a team as a code owner September 18, 2021 02:00
tests/tr1/tests/cstdio/test.cpp Show resolved Hide resolved
tests/tr1/tests/cstdio/test.cpp Outdated Show resolved Hide resolved
tests/tr1/tests/cwchar1/test.cpp Outdated Show resolved Hide resolved
@StephanTLavavej
Copy link
Member Author

I'm mirroring this to an MSVC-internal PR. Please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit dc888f7 into microsoft:main Sep 25, 2021
@StephanTLavavej StephanTLavavej deleted the temp-names branch September 25, 2021 03:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test Related to test code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants