[Google Pixel 2 XL Guides, News, & Discussion] MartyMacGyver:
SOLVED!

Adding NathanChance as this is a very useful guide, it just needs a couple of VERY important updates.

Also adding Dees_Troy from TWRP. Maybe this is a bug or by design, but this all revolves around a subtle interaction with TWRP.

"Well, %*#@%." <----

I said that aloud and it bears repeating here. Why? Because I figured out the problem and after several hours of this I'd really like others to know how to avoid it.

First of all, there's a mistake in the guide:

adb pull /sdcard/MagiskManager/patched_boot.img

should be:

adb pull /sdcard/Download/patched_boot.img

Interesting point: I had a patched_boot.img file in the MagiskManager folder, apparently from an old version or something. It was outdated. I don't know if it was getting selected at any point but it's worth making sure you install the correct one and definitely NOT some old, previously patched one!

I fixed that problem about two hours ago, so that's not the "Eureka!" moment. No, it's the fact that TWRP (twrp-3.2.3-1-taimen.img) defaults to installing itself to the system when you exit it! While that seems to be in the world of 8.1 (at least I never noticed it being a problem there) it causes big problems in 9.0 apparently. I clicked OK at least once along the way (after re-flashing the factory image)... and that led to all this trouble.

I started looking at the logcat output during the long attempts to boot (if Android starts drawing that line under the Googoe logo signifying it's working, that means it's having issues):

adb logcat -d > logcat.txt

That led to some interesting output:

11-03 16:40:55.970 1787 1787 E AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: main
11-03 16:40:55.970 1787 1787 E AndroidRuntime: java.lang.IllegalStateException: Signature|privileged permissions not in privapp-permissions whitelist: {me.twrp.twrpapp: android.permission.READ_PRECISE_PHONE_STATE, me.twrp.twrpapp: android.permission.BATTERY_STATS, me.twrp.twrpapp: android.permission.PACKAGE_USAGE_STATS}
11-03 16:40:55.970 1787 1787 E AndroidRuntime:	at com.android.server.pm.permission.PermissionManagerService.systemReady(PermissionManagerService.java:2005)
......

At that point, not finding any good info on how to rip it out cleanly (it wasn't in /data or /data/data and I couldn't find anything in /system (all using the TWRP file explorer)) I re-flashed the factory image for the third time today. THEN, having already created a patched boot image earlier (which was failing to work up to now) I flashed that patched image to the phone.

It worked.

The subsequent logcat during boot showed this:

....
11-03 16:50:16.206 1168 1168 W PackageManager: System package me.twrp.twrpapp no longer exists; it's data will be wiped
....
11-03 16:50:16.754 1168 1168 W PackageManager: Destroying /data/user_de/0/me.twrp.twrpapp due to: com.android.server.pm.PackageManagerException: Package me.twrp.twrpapp is unknown
....

The log from next reboot didn't mention TWRP at all.

And after all that, I have root.

Key points:

If you use TWRP at any point to flash anything while trying to root the phone, DO NOT let it install the TWRP app. You can do that later on when things are stable and rooted.

Don't grab the wrong `patched_boot.img` if patching via the Magisk app.

EDIT: Magisk modules can cause problems when upgrading - you should uninstall them before rooting if you have any issues. (I didn't have any extra modules installed so it wasn't relevant to me.)

Magisk 17.3 + Magisk Manager 6.0.1 works fine and is probably preferable to any earlier versions. SafetyNet Check succeeds.

Security was not a factor here. You don't need to disable it.

Encryption was not a factor here. This all works fine with encryption.

The kernel was not a factor here. Stock works fine. Try custom kernels if you want AFTER you get root working.

Edit: Technically, wiping partitions/re-flashing the factory image helps, but ONLY if you don't install the TWRP app again! If you allow that to happen by default, the problems will continue. That's why this was so vexing - I'd re-flash factory, try to use TWRP to uninstall/reinstall Magisk, and let it TWRP install its app (I'd had it installed before without incident so it flew under the radar).

Turn the screen timeout in TWRP OFF - for some reason if the screen times out, it never wakes back up. It was another needless distraction.

Understand that TWRP and Magisk both modify the boot image. I'd suggest you install Magisk first (if using TWRP, use fastboot to temporarily boot with it), DON'T install the app via TWRP! Later, if you want it, run TWRP again and make it the default recovery (haven't tried it yet but it should work).

In short, it was a very deterministic problem with a very deterministic solution. No wonder people have either had complete success or mysterious failures / multiple tries that just happen to finally work. I feel really bad for anyone who went that extra step and wiped their data because of a lack of information.

I opened a ticket with TWRP regarding this as well:

https://github.com/TeamWin/Team-Win-Recovery-Project/issues/1347