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

Change directory to $*TMPDIR #732

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

Kaiepi
Copy link
Contributor

@Kaiepi Kaiepi commented Apr 10, 2021

Fixes #718

Kaiepi added a commit to Kaiepi/roast that referenced this pull request Apr 10, 2021
@Kaiepi Kaiepi force-pushed the unix-path-length branch 2 times, most recently from e806121 to 202dc89 Compare April 10, 2021 11:12
@Kaiepi Kaiepi changed the title Change directory to $*TMPDIR and unlink closed UNIX sockets Change directory to $*TMPDIR Apr 10, 2021
my IO::Socket::INET:_ $server;
my IO::Socket::INET:_ $client;
my IO::Socket::INET:_ $accepted;
my Str:D $host = $*TMPDIR.add("test-$*PID.sock").Str;
my Str:D $host = "./test-$*PID.sock";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest a bit less generic name here. However little the probability for an entry with the same name to exists in the temp dir, I'd rather make it even smaller. To my view, the 100% reliable solution (unless race condition is an option) is to add just one line:

$host = "./test-$*PID-" ~ ++$ ~ ".sock" while -e $host;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, there is a risk of race conditions when checking in that way, unfortunately.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I meant when added the "(unless ...)" part. But at least this problem is way less probable than with a static rather predictable name.

Copy link
Contributor Author

@Kaiepi Kaiepi Apr 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured existing I/O tests would already have dealt with this sort of problem. S16-filehandles/io.t opens files in the current directory and uses a less predictable filename:

sub nonce () { return ".{$*PID}." ~ (1..1000).pick() }
my $filename = 'tempfile_filehandles_io' ~ nonce();

But will silently continue with tests and remove the files if they already exist. I'm a bit wary of reusing this code. Do we always have permissions to write there?

If a file at the path already exists, tests will fail. We can't check
for the existence of the file beforehand (racy!), so make it a little
more difficult for that to happen in the same way S16-filehandles/io.t
does for its test files.
@niner
Copy link
Contributor

niner commented Apr 11, 2021 via email

@vrurg
Copy link
Contributor

vrurg commented Apr 11, 2021

My bad, I haven't done it in first place, but a search in packages/Test-Helpers came up with make-temp-* family of routines in Test::Util. Moreover, they record all files/dirs created and wipe them out in END phaser.

This deduplicates some temporary file logic. Basenames generated by this
routine are rather long (around 70 characters), but being in $*TMPDIR,
we can use a relative path to give us more wiggle room.
my IO::Socket::INET:_ $server;
my IO::Socket::INET:_ $client;
my IO::Socket::INET:_ $accepted;
my Str:D $host = $*TMPDIR.add("test-$*PID.sock").Str;
my Str:D $host = "./test-$*PID.sock";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I meant when added the "(unless ...)" part. But at least this problem is way less probable than with a static rather predictable name.

MasterDuke17 added a commit to MasterDuke17/roast that referenced this pull request Jul 17, 2021
The TODO is now passing, and the new exception thrown is a better one.
MasterDuke17 added a commit that referenced this pull request Jul 19, 2021
Altai-man pushed a commit that referenced this pull request Jul 24, 2021
The TODO is now passing, and the new exception thrown is a better one.
Altai-man pushed a commit that referenced this pull request Aug 21, 2021
The TODO is now passing, and the new exception thrown is a better one.
@usev6
Copy link
Contributor

usev6 commented Feb 5, 2023

I tried out the patch and I'm afraid it doesn't really work as intended. For me (on FreeBSD 12.4) the socket files are created in the directory where I start rakudo-m -- and not in $*TMPDIR.

I'm not totally sure about the why, but from what I've seen the C code that actually creates the socket file (IIUC it's the bind in https://github.com/MoarVM/MoarVM/blob/6b456a6c05/src/io/syncsocket.c#L453) has its current working directory unchanged -- it's still the directory where I started rakudo-m. Since bind only gets a (relative) file name in dest->sa_data the socket file is not created in /tmp, but in the "start directory".

But maybe we should just use an absolute path instead of a relative one. (There's still the potential problem with $*TMPDIR already having a long path, but that's probably manageable by the user who wants to run the tests.) I'll open an alternative PR ...

usev6 added a commit to usev6/roast that referenced this pull request Feb 5, 2023
This deduplicates some temporary file logic (and probably generates
"better" file names).

The main part of the patch was written by @Kaiepi -- see
Raku#732. The only difference in this
commit is the usage of an absolute path, instead of changing to
$*TMPDIR and using a relative path. That's necessary because the
C code that actually creates the socket file doesn't get its
current working directory changed.
@usev6
Copy link
Contributor

usev6 commented Feb 5, 2023

Alternative PR: #829

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

S32-io/IO-Socket-INET-UNIX.t is busted
4 participants