Skip to content

Fix potential process-wide umask mixups - #1449

Merged
kurtmckee merged 1 commit into
mainfrom
fix-potential-umask-mixups
Nov 12, 2026
Merged

kurtmckee merged 1 commit into
mainfrom
fix-potential-umask-mixups

Conversation

@kurtmckee

Copy link
Copy Markdown
Member

Deprecated

  • FileTokenStorage.user_only_umask() and FileAdapter.user_only_umask()
    are deprecated and will be removed in version 5. (:pr:NUMBER)

    They change the umask of the whole process,
    which is not safe when other threads are running.

Fixed

  • All file-based token storage classes create files
    with mode 0o600 set directly, and do not change the process umask. (:pr:NUMBER)

    This avoids a thread safety issue that could cause I/O in other threads
    to create files with an unexpected umask.

@sirosen sirosen 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.

  1. Three cheers for the fix itself, in src/!
  2. Request for some added doc in test utilities.
  3. If we can find a way to reduce test duplication, I would like that. But if it seems impossible, we can set that aside.

assert stat.S_IMODE(json_file.stat().st_mode) == 0o600


@pytest.mark.xfail(IS_WINDOWS, reason="cannot set umask perms on Windows")

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.

xfail or skip? I would have naively reached for a skip, so I'm curious if there's a particular benefit in having these tests as xfail that I should think about and learn.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Keeping this as an xfail makes the code run, which gives useful feedback if this starts passing unexpectedly.

A skip would mean that we don't get that signal.

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.

Sure... I guess that's all there is to it? I was thinking "but it will never pass!"
I guess your point is that that's an assumption, and xfail gives us the power to verify it.

I'll think more about the cases in which I skip tests. Maybe I need to make a broader realignment.

with open(json_file, "w", opener=JSONTokenStorage._owner_only_opener) as f:
f.write("data")
assert stat.S_IMODE(json_file.stat().st_mode) == 0o600
assert json_file.read_text() == "data"

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.

It reads to me like we have the same tests in duplicate for the current ("v2") token storage and the legacy storage. Are we comfortable simply removing the legacy testing path? Or can we do something to combine these?

I count, as it stands, four variants of test_..._leave_the_process_umask_alone. All are testing the same invariant, across four different classes.

It strikes me as rather a lot of not only identical physical lines, but a lot of identical ideas, so it's registering as something to improve.

If the code under test serializes the writes, the second can never reach its
checkpoint while the first waits, so the first simply times out and carries on.
The order of events is then different, but nothing deadlocks.
"""

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.

Can we add a little console block here to show demo usage? I really like this class, but it took a few minutes to read through usage sites and understand it.

We don't have to work with a real use-case, just something like...

# rephrase further as you see fit
controller = OverlappingWrites()
action1 = build_action1(callback=controller.first_checkpoint)
action2 = build_action2(callback=controller.second_checkpoint)
controller.run(action1, action2)

So that it's clear that the OverlappingWrites object lifecycle wraps around the code under test.

@kurtmckee
kurtmckee force-pushed the fix-potential-umask-mixups branch from 6dde763 to e0e6f73 Compare November 12, 2026 22:24
@codspeed

codspeed Bot commented Nov 12, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 15.44%

1 warning

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 1 improved benchmark
✅ 17 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_deeply_nested_object_encoding[5w-1d] 173.5 µs 150.3 µs +15.44%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing fix-potential-umask-mixups (e0e6f73) with main (40ce7d4)

Open in CodSpeed

@sirosen sirosen 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.

I think we could find ways to consolidate more of the tests, but I think this is at a good point for the simplicity-to-concision tradeoff.

@kurtmckee
kurtmckee merged commit 69c3cce into main Nov 12, 2026
12 checks passed
@kurtmckee
kurtmckee deleted the fix-potential-umask-mixups branch November 12, 2026 23:51
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.

2 participants