Bug 249354

Summary: ports-mgmt/pkg: pkg upgrade re-spawns continuously when using multiple repositories
Product: Ports & Packages Reporter: Daniel Morante <daniel>
Component: Individual Port(s)Assignee: freebsd-pkg (Nobody) <pkg>
Status: New ---    
Severity: Affects Many People CC: pkg
Priority: --- Flags: linimon: maintainer-feedback? (pkg)
Version: Latest   
Hardware: Any   
OS: Any   

Description Daniel Morante 2020-09-16 01:21:36 UTC
Summary

The pkg tool will continuously re-spawn itself during a 'pkg upgrade' when a new version of pkg is available in the following setup:

- Poudriere on a dedicated machine is used to build custom pkg repositories
- Servers with FreeBSD that uses modern pkg, the version is irrelevant.
- [May be irrelevant] Servers have disabled the standard repository.
- Servers have are configured with two custom repositories.

Steps on how to reproduce

The contents of the repositories created with Poudriere do not matter.  Whether it's a complete build of all packages or a specific list, the described behavior still occurs.

1) [optional?] Disable the FreeBSD default repository by placing a file in `/usr/local/etc/pkg/repos/FreeBSD.conf` with the following contents:

```
FreeBSD: { enabled: no }
```

2) Create two pkg repositories with Poudriere:

```
# Build and clean Repo1
poudriere bulk -j 12amd64 -z repo1 -f repo1_list -p default
poudriere pkgclean -y -j 12amd64 -z repo1 -f repo1_list  -p default

# Build and clean Repo1
poudriere bulk -j 12amd64 -z repo2 -f repo2_list -p default
poudriere pkgclean -y -j 12amd64 -z repo2 -f repo2_list  -p default
```

3) Add the two repositories to the FreeBSD client systems:

`/usr/local/etc/pkg/repos/repo1.conf`:
```
repo1: {
	url: "http://pkg.domain.tld/repo1/${ABI}",
	enabled: yes
}
```
`/usr/local/etc/pkg/repos/repo2.conf`:
```
repo2: {
	url: "http://pkg.domain.tld/repo2/${ABI}",
	enabled: yes
}
```

4) Bootstrap pkg, install and use package as you normally would.
5) On the pkg repository system, update ports tree and build repositories (as done in step 2).
6) On FreeBSD client systems run `pkg upgrade`.
7) The `pkg upgrade` command continuously re-spawns itself:

```
root@freebsd-client:~ # pkg upgrade
Updating repo1 repository catalogue...
repo1 repository is up to date.
Updating repo2 repository catalogue...
repo2 repository is up to date.
All repositories are up to date.
New version of pkg detected; it needs to be installed first.
Checking integrity... done (0 conflicting)
Your packages are up to date.
Updating repo1 repository catalogue...
repo1 repository is up to date.
Updating repo2 repository catalogue...
repo2 repository is up to date.
All repositories are up to date.
New version of pkg detected; it needs to be installed first.
Checking integrity... done (0 conflicting)
Your packages are up to date.
Updating repo1 repository catalogue...
repo1 repository is up to date.
Updating repo2 repository catalogue...
repo2 repository is up to date.
All repositories are up to date.
New version of pkg detected; it needs to be installed first.
Checking integrity... done (0 conflicting)
Your packages are up to date.
...
```

8) Look as process listing `ps dauxww` and you will see multiple child processes of `pkg upgrade`.  Note this also occurs if you specify a package name ie. "pkg upgrade <package>"

```
root  1433   0.0  0.8  13176   7940  -  Ss   22:02    0:00.03 | `-- sshd: root@pts/1 (sshd)
root  1435   0.0  0.4   7396   3788  1  Ss   22:02    0:00.02 |   `-- -csh (csh)
root  1438   0.0  0.9  13912   8792  1  S+   22:02    0:00.01 |     `-- pkg upgrade
root  1440   0.0  0.9  13912   8792  1  S+   22:02    0:00.01 |       `-- pkg upgrade
root  1442   0.0  0.9  13912   8792  1  S+   22:02    0:00.01 |         `-- pkg upgrade
root  1444   0.0  0.9  13912   8792  1  S+   22:02    0:00.01 |           `-- pkg upgrade
root  1447   0.0  0.9  13912   8792  1  S+   22:02    0:00.01 |             `-- pkg upgrade
root  1449   0.0  0.9  13912   8792  1  S+   22:02    0:00.01 |               `-- pkg upgrade
root  1451   0.0  0.9  13912   8792  1  S+   22:02    0:00.01 |                 `-- pkg upgrade
root  1453   0.0  0.9  13912   8792  1  S+   22:02    0:00.01 |                   `-- pkg upgrade
root  1456   0.0  0.9  13912   8792  1  S+   22:02    0:00.01 |                     `-- pkg upgrade
root  1458   0.0  0.9  13912   8792  1  S+   22:02    0:00.01 |                       `-- pkg upgrade
root  1460   0.0  0.9  13912   8792  1  S+   22:02    0:00.01 |                         `-- pkg upgrade
root  1462   0.0  0.9  13912   8792  1  S+   22:02    0:00.01 |                           `-- pkg upgrade
root  1464   0.0  0.9  13912   8792  1  S+   22:02    0:00.01 |                             `-- pkg upgrade
root  1465   0.0  1.1  15964  11628  1  D+   22:02    0:00.02 |                               `-- pkg upgrade
```

9) If this is allowed to continue, all system memory will be consumed, swap will eventually run out, and the OS will become unresponsive.

```
Sep 15 12:18:13 freebsd-client kernel: swap_pager_getswapspace(18): failed
Sep 15 12:18:13 freebsd-client kernel: swap_pager_getswapspace(32): failed
Sep 15 12:18:13 freebsd-client kernel: swap_pager_getswapspace(24): failed
Sep 15 12:18:13 freebsd-client kernel: swap_pager_getswapspace(18): failed
Sep 15 12:18:13 freebsd-client kernel: swap_pager_getswapspace(14): failed
Sep 15 12:18:13 freebsd-client kernel: swap_pager_getswapspace(8): failed
Sep 15 12:18:14 freebsd-client kernel: swap_pager_getswapspace(11): failed
Sep 15 12:18:14 freebsd-client kernel: swap_pager_getswapspace(32): failed
Sep 15 12:18:14 freebsd-client kernel: swap_pager_getswapspace(28): failed
Sep 15 12:18:14 freebsd-client kernel: swap_pager_getswapspace(21): failed
Sep 15 12:18:14 freebsd-client kernel: swap_pager_getswapspace(16): failed
Sep 15 12:18:14 freebsd-client kernel: swap_pager_getswapspace(12): failed
Sep 15 12:18:14 freebsd-client kernel: swap_pager_getswapspace(9): failed
Sep 15 12:18:14 freebsd-client kernel: swap_pager_getswapspace(9): failed
Sep 15 12:18:14 freebsd-client kernel: swap_pager_getswapspace(7): failed
Sep 15 12:18:14 freebsd-client kernel: swap_pager_getswapspace(4): failed
Sep 15 12:18:14 freebsd-client kernel: swap_pager_getswapspace(4): failed
Sep 15 12:18:14 freebsd-client kernel: swap_pager_getswapspace(6): failed
Sep 15 12:18:14 freebsd-client kernel: swap_pager_getswapspace(3): failed
```

10) To prevent the endless re-spawn, You must upgrade pkg from any repository by explicitly specifying one:

```
# pkg upgrade -r repo1 pkg
Updating repo1 repository catalogue...
repo1 repository is up to date.
All repositories are up to date.
New version of pkg detected; it needs to be installed first.
The following 1 package(s) will be affected (of 0 checked):

Installed packages to be UPGRADED:
        pkg: 1.14.7 -> 1.15.4 [repo1]

Number of packages to be upgraded: 1

3 MiB to be downloaded.

Proceed with this action? [y/N]:

```

-or-

```
# pkg upgrade -r repo2 pkg
Updating repo2 repository catalogue...
repo2 repository is up to date.
All repositories are up to date.
New version of pkg detected; it needs to be installed first.
The following 1 package(s) will be affected (of 0 checked):

Installed packages to be UPGRADED:
        pkg: 1.14.7 -> 1.15.4 [repo2]

Number of packages to be upgraded: 1

3 MiB to be downloaded.

```

11) After which the problem stops until a new version of pkg is once again made available in the repositories

```
...
Proceed with this action? [y/N]: y
[1/1] Fetching pkg-1.15.4.txz: 100%    3 MiB   1.8MB/s    00:02
Checking integrity... done (0 conflicting)
[1/1] Upgrading pkg from 1.14.7 to 1.15.4...
[1/1] Extracting pkg-1.15.4: 100%
You may need to manually remove /usr/local/etc/pkg.conf if it is no longer needed.
Updating repo2 repository catalogue...
repo2 repository is up to date.
All repositories are up to date.
Checking integrity... done (0 conflicting)
Your packages are up to date.

# pkg upgrade
Updating repo1 repository catalogue...
repo1 repository is up to date.
Updating repo2 repository catalogue...
repo2 repository is up to date.
All repositories are up to date.
Checking for upgrades (23 candidates): 100%
Processing candidates (23 candidates): 100%
The following 23 package(s) will be affected (of 0 checked):

Installed packages to be UPGRADED:
...
```