Skip to content

POSIX: Keep original timeout from sleep call #878

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

Merged
merged 1 commit into from
Jun 7, 2025

Conversation

etcwilde
Copy link
Member

@etcwilde etcwilde commented Jun 3, 2025

The dispatch_sema4_timedwait would reset the _timeout to an absolute time after the sleep was interrupted instead of when the sleep was called. Interrupting the process while it was sleeping would result in the new absolute timeout deadline being computed using the timeout offset from the time of the interrupt. e.g. if the timeout is 10 seconds, it will be ten seconds from when the process was interrupted because the absolute deadline was recomputed. Interrupting the process repeatedly while sleeping would will make the process go back to sleep instead of waking up when the original absolute deadline was reached.

timeout is a relative timeout offset. nsec and _timeout are absolute times since the epoch specifying when the wait should stop.

The `dispatch_sema4_timedwait` would reset the `_timeout` to an absolute
time after the sleep was interrupted instead of when the sleep was
called. Interrupting the process while it was sleeping would result in
the new absolute timeout deadline being computed using the `timeout`
offset from the time of the interrupt. e.g. if the timeout is 10
seconds, it will be ten seconds from when the process was interrupted
because the absolute deadline was recomputed. Interrupting the process
repeatedly while sleeping would will make the process go back to sleep
instead of waking up when the original absolute deadline was reached.

`timeout` is a relative timeout offset. `nsec` and `_timeout` are
absolute times since the epoch specifying when the wait should stop.
@etcwilde
Copy link
Member Author

etcwilde commented Jun 3, 2025

@swift-ci please test

Copy link
Contributor

@al45tair al45tair left a comment

Choose a reason for hiding this comment

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

LGTM.

@etcwilde
Copy link
Member Author

etcwilde commented Jun 3, 2025

Windows and Apple have a similar sequence. I don't know what values are going through the corresponding functions well enough to feel comfortable applying this change on the other platforms. If they are working with absolute deadlines, then those implementations should likely have this fix applied as well.

@etcwilde
Copy link
Member Author

etcwilde commented Jun 3, 2025

@swift-ci please test Windows

@etcwilde
Copy link
Member Author

etcwilde commented Jun 3, 2025

Windows failure: FileNotFoundError: [WinError 206] The filename or extension is too long

[86/389][ 22%][2.146s] Compiling T:/x86_64-unknown-windows-msvc/Runtime/stdlib/public/core//WINDOWS/x86_64/Swift.obj
FAILED: stdlib/public/core/WINDOWS/x86_64/Swift.obj T:/x86_64-unknown-windows-msvc/Runtime/stdlib/public/core/WINDOWS/x86_64/Swift.obj 
stdlib\public\core\CMakeFiles\Swift.obj-55efed0.bat 04dd65cdcd07fb28
Traceback (most recent call last):
  File "C:\Users\swift-ci\jenkins\workspace\swift-corelibs-libdispatch-PR-windows\swift\utils\line-directive", line 738, in <module>
    run()
  File "C:\Users\swift-ci\jenkins\workspace\swift-corelibs-libdispatch-PR-windows\swift\utils\line-directive", line 689, in run
    command = subprocess.Popen(
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\subprocess.py", line 1420, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 206] The filename or extension is too long
Batch file failed at line 4 with errorcode 1

[87/389][ 22%][2.219s] Generating T:/x86_64-unknown-windows-msvc/Runtime/./lib/swift/windows/Swift.swiftmodule/x86_64-unknown-windows-msvc.swiftmodule
FAILED: lib/swift/windows/Swift.swiftmodule/x86_64-unknown-windows-msvc.swiftmodule lib/swift/windows/Swift.swiftmodule/x86_64-unknown-windows-msvc.swiftdoc lib/swift/windows/Swift.swiftmodule/x86_64-unknown-windows-msvc.swiftinterface lib/swift/windows/Swift.swiftmodule/x86_64-unknown-windows-msvc.private.swiftinterface T:/x86_64-unknown-windows-msvc/Runtime/lib/swift/windows/Swift.swiftmodule/x86_64-unknown-windows-msvc.swiftmodule T:/x86_64-unknown-windows-msvc/Runtime/lib/swift/windows/Swift.swiftmodule/x86_64-unknown-windows-msvc.swiftdoc T:/x86_64-unknown-windows-msvc/Runtime/lib/swift/windows/Swift.swiftmodule/x86_64-unknown-windows-msvc.swiftinterface T:/x86_64-unknown-windows-msvc/Runtime/lib/swift/windows/Swift.swiftmodule/x86_64-unknown-windows-msvc.private.swiftinterface 
stdlib\public\core\CMakeFiles\x86_64-unknown-windows-msvc.swiftmodule-dc66939.bat 131ae00f606c0aa8
Traceback (most recent call last):
  File "C:\Users\swift-ci\jenkins\workspace\swift-corelibs-libdispatch-PR-windows\swift\utils\line-directive", line 738, in <module>
    run()
  File "C:\Users\swift-ci\jenkins\workspace\swift-corelibs-libdispatch-PR-windows\swift\utils\line-directive", line 689, in run
    command = subprocess.Popen(
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\subprocess.py", line 1420, in _execute_child

    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,

FileNotFoundError: [WinError 206] The filename or extension is too long

Batch file failed at line 6 with errorcode 1

@al45tair
Copy link
Contributor

al45tair commented Jun 4, 2025

Windows and Apple have a similar sequence. I don't know what values are going through the corresponding functions well enough to feel comfortable applying this change on the other platforms. If they are working with absolute deadlines, then those implementations should likely have this fix applied as well.

I think both of the other implementations call functions that take a wait duration, not a timestamp.

@etcwilde
Copy link
Member Author

etcwilde commented Jun 4, 2025

bool
_dispatch_sema4_timedwait(_dispatch_sema4_t *sema, dispatch_time_t timeout)
{
	mach_timespec_t _timeout;
	kern_return_t kr;

	do {
		uint64_t nsec = _dispatch_timeout(timeout);
		_timeout.tv_sec = (__typeof__(_timeout.tv_sec))(nsec / NSEC_PER_SEC);
		_timeout.tv_nsec = (__typeof__(_timeout.tv_nsec))(nsec % NSEC_PER_SEC);
		kr = semaphore_timedwait(*sema, _timeout);
	} while (unlikely(kr == KERN_ABORTED));

	if (kr == KERN_OPERATION_TIMED_OUT) {
		return true;
	}
	DISPATCH_SEMAPHORE_VERIFY_KR(kr);
	return false;
}

Unfortunately, semaphore_timedwait doesn't seem to have documentation either on developer.apple.com nor in the SDK, so I don't know if it's a relative time, but I think you might be right given _dispatch_timeout constructing the timeout.

@al45tair
Copy link
Contributor

al45tair commented Jun 4, 2025

Unfortunately, semaphore_timedwait doesn't seem to have documentation either on developer.apple.com nor in the SDK, so I don't know if it's a relative time

It is definitely a relative time (I checked).

@compnerd
Copy link
Member

compnerd commented Jun 6, 2025

@swift-ci please test Windows platform

@al45tair
Copy link
Contributor

al45tair commented Jun 6, 2025

@swift-ci Please test Windows platform

@etcwilde
Copy link
Member Author

etcwilde commented Jun 6, 2025

@swift-ci please test Windows platform

1 similar comment
@etcwilde
Copy link
Member Author

etcwilde commented Jun 6, 2025

@swift-ci please test Windows platform

@etcwilde
Copy link
Member Author

etcwilde commented Jun 7, 2025

swiftlang/swift#82074
@swift-ci please test Windows platform

@etcwilde etcwilde merged commit 3e2dc99 into swiftlang:main Jun 7, 2025
2 checks passed
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.

4 participants