Skip to content

fix: unlock the OS thread when ScopeUserByIds fails - #899

Merged
butonic merged 1 commit into
opencloud-eu:mainfrom
pbleser-oc:fix/scope-user-by-ids-unlock-thread
Oct 26, 2026
Merged

butonic merged 1 commit into
opencloud-eu:mainfrom
pbleser-oc:fix/scope-user-by-ids-unlock-thread

Conversation

@pbleser-oc

@pbleser-oc pbleser-oc commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

UnixMapper.ScopeUserByIds locks the goroutine to its OS thread (runtime.LockOSThread) before it switches the fsuid and the fsgid, as they belong to the thread, not to the goroutine. The function it returns restores them and unlocks the thread again.

On an error, though, it returned without that function and without unlocking:

  • the goroutine stayed locked to its thread for good;
  • a fsuid it had already switched before switching the fsgid failed was not restored.

For the short-lived goroutine of a request, that is mostly harmless: Go discards a locked thread when its goroutine ends. Long-lived goroutines, though, would stay pinned to their thread after a single failure, with one more lock nested for every further one. The background workers of the revision cleanup proposed in #895 are such goroutines: with use_space_groups, they switch to the group of a space for every file. Split off from #895, as suggested in its review.

Changes

ScopeUserByIds now restores the fsuid and unlocks the thread on every error path. The comments explain why the unlock is needed.

Tests

The fsuid and fsgid are restored after switching, and unchanged after a failed switch to a group the process is not in. The latter is skipped where the switch succeeds, e.g. when running as root or with CAP_SETGID. Whether the thread was unlocked can't be tested: Go has no API to query whether a goroutine is locked to its thread.

@aduffeck aduffeck left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nice find, lgtm 👍 (except the utils/decomposedfs changes)

UnixMapper.ScopeUserByIds locks the goroutine to its OS thread before it
switches the fsuid and the fsgid, as they belong to the thread. The
function it returns restores them and unlocks the thread again. On an
error, though, it returned without that function and without unlocking:
the goroutine stayed locked to its thread for good, and a fsuid it had
already switched before the fsgid failed was not restored.

For a short-lived goroutine of a request, Go discards the locked thread
when the goroutine ends. Long-lived goroutines would stay pinned to their
thread after a single failure, with one more lock nested for every
further one. The background workers of the revision cleanup proposed in
opencloud-eu#895 are such goroutines: with use_space_groups, they
switch to the group of a space for every file.

ScopeUserByIds now restores the fsuid and unlocks the thread on every
error path.

Tests: the fsuid and fsgid are restored after switching, and unchanged
after a failed switch to a group the process is not in (skipped where
the switch succeeds, e.g. as root). Whether the thread was unlocked
can't be observed from Go, there is no API to query it.
@pbleser-oc
pbleser-oc force-pushed the fix/scope-user-by-ids-unlock-thread branch from 25f4981 to e6ece73 Compare October 23, 2026 06:49
@butonic
butonic merged commit 36ca577 into opencloud-eu:main Oct 26, 2026
19 checks passed
@github-project-automation github-project-automation Bot moved this from Qualification to Done in OpenCloud Team Board Oct 26, 2026
@openclouders openclouders mentioned this pull request Oct 26, 2026
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants