Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 900226 - Portage creates binpkgs in PKGDIR, when it should use a temp dir and move the finished build binpkg later into PKGDIR
Summary: Portage creates binpkgs in PKGDIR, when it should use a temp dir and move the...
Status: CONFIRMED
Alias: None
Product: Portage Development
Classification: Unclassified
Component: Binary packages support (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Portage team
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-03-08 04:14 UTC by Sam James
Modified: 2024-06-19 21:33 UTC (History)
6 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sam James archtester Gentoo Infrastructure gentoo-dev Security 2023-03-08 04:14:47 UTC
I ^C quite often apparently and often end up with noise like this:
```
>>> Jobs: 0 of 3 complete, 3 running                Load avg: 0.24, 0.09, 0.52Error reading binpkg '/var/cache/binpkgs/dev-util/pkgcheck/pkgcheck-0.10.23-1.gpkg.tar': [Errno 22] Invalid argument!!! Invalid binary package: '/var/cache/binpkgs/dev-util/pkgcheck/pkgcheck-0.10.23-1.gpkg.tar', Error reading binpkg '/var/cache/binpkgs/dev-util/pkgcheck/pkgcheck-0.10.23-1.gpkg.tar': [Errno 22] Invalid argument
Error reading binpkg '/var/cache/binpkgs/dev-util/pkgcheck/pkgcheck-0.10.23-1.gpkg.tar': [Errno 22] Invalid argument!!! Invalid binary package: '/var/cache/binpkgs/dev-util/pkgcheck/pkgcheck-0.10.23-1.gpkg.tar', Error reading binpkg '/var/cache/binpkgs/dev-util/pkgcheck/pkgcheck-0.10.23-1.gpkg.tar': [Errno 22] Invalid argument

```

The output is pretty ugly so we should fix that, but the main issue is that these invalid binpkgs are left behind.

I think what happens is:
1. Emerge starts
2. Install completes and Portage starts to create a binpkg (possibly invoking a slow compressor) at the *real* path like /var/cache/binpkgs/app-misc/foo/foo-1.gpkg.tar (no temporary suffix)
3. Interruption during this process leads to a broken binpkg being left at the real path

We should instead write to a .tmp or .partial then rename once the process is complete (even just the compressor is likely enough).
Comment 1 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2023-03-08 04:15:59 UTC
Note that I didn't heavily use xpak but use gpkg now, and I suspect it might be more likely with gpkg..?
Comment 2 Sheng Yu 2023-03-08 04:25:44 UTC
That file maybe a placeholder, the temporary files should already had a suffix when creating. Or it get interrupted in the post-process.
Comment 3 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2023-03-08 06:03:46 UTC
(In reply to Sheng Yu from comment #2)
> That file maybe a placeholder, the temporary files should already had a
> suffix when creating. Or it get interrupted in the post-process.

Ah, I see. Yeah, placeholder or interrupted makes sense. It's 0 bytes for me.
Comment 4 Florian Schmaus gentoo-dev 2023-09-07 08:11:46 UTC
I run into a similar issue that (probably) has the same root cause.

While emerging a package I got

Error reading binpkg '/var/cache/binpkgs/net-im/profanity/profanity-0.14.0-r1-1.gpkg.tar': [Errno 22] Invalid argument

while profanity got simultaneously emerged.

So it appears that portages creates the binpkgs in PKGDIR.

Instead, portage should use a temp dir when creating binpkgs and once finished, move the result into PKGDIR. Or, if portage got interrupted, the tempdir should be deleted.

This would probably fix the original misbehavior reported in this bug and what I experienced.
Comment 5 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2023-09-28 13:36:42 UTC
I caught this at perhaps a better point today:

-rw-r--r--  1 root root    0 Sep 28 14:34 gcc-12.3.1_p20230922-3.gpkg.tar
-rw-r--r--  1 root root  39K Sep 28 14:34 gcc-12.3.1_p20230922-3.gpkg.tar.688064

We do create a placeholder and we put the gunk in there while we're compresssing ... but we *also* create a placeholder at the real filename!
Comment 6 Mike Gilbert gentoo-dev 2023-10-02 20:53:01 UTC
I thinks this summarizes the issue:

The gpkg format requires we create a directory matching the name of the package file. For example, baselayout-2.14-4.gpkg.tar must contain a directory named baselayout-2.14-4.

To avoid race conditions with multiple Portage processes writing to PKGDIR, we need to "claim" a package file name before actually writing to it.

We claim the package file name by creating an empty file in PKGDIR. That empty file is what produces the "invalid binary package".

I think the only workable solution here is to ensure we remove the empty file when errors occur or when we get interrupted by a signal.
Comment 7 Mike Gilbert gentoo-dev 2023-10-02 20:59:36 UTC
> I think the only workable solution here is to ensure we remove the empty file when errors occur or when we get interrupted by a signal.

Or possibly we could switch to using some other file pattern or locking mechanism to claim/reserve a package filename while the package is being created.