fix(ssl): release connections when their context terminates #8541
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8541"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-20T15:52:00Z.
What
Terminating an SSL context removes the context but retains its connection records. Later connection lookup rejects their missing parent, so
sceSslDeleteConnectionreturns0x8095F006and cannot release the orphaned registry entries. Three Init/Create/Term/Delete cycles leave three records on the tested main.Remove the terminating context's connections under the existing registry mutex before releasing the context. Existing shared session ownership lets operations already in progress finish and keeps their memory pool alive until the last operation releases it. Calls made after termination keep returning the existing invalid-ID result. Record removal uses
std::erase_ifwithout allocating a temporary container. Idle sessions, including a last native socket owner, can be destroyed while the registry mutex is held.Remove the two obsolete ownership clauses from
docs/dev/debt/libSceSsl.mdwhile preserving the console-measured invalid-ID returns and the remaining open gaps.Add a lifecycle regression that compiles the actual SSL implementation into its test translation unit to observe private ownership. It checks both cleanup orders, multiple contexts, repeated concurrent create/delete/term operations and a paused verification callback with initialized TLS backend state. Related to #8005, finding C10.
Tested
Rebase validation on Windows 11 Pro 10.0.26300, WinLibs MinGW-w64 GCC 15.2.0r7 UCRT, Release/O3,
BUILD_TESTING=ON, full runtime configuration. CPU: AMD Ryzen 9 7900X; RAM: 32 GB; GPU: NVIDIA GeForce RTX 4070 Ti SUPER.Validated base:
a6dcea9279b53b3a39c13695eac66f7c8b5b503c. Rebased head:f2173bba34562299f9d90583120ade2205ae12ef. Both original commits are retained. All production build targets and selected fixtures pass without an overlay. All 20 selected CTests pass without skips in 8.42 s, including all six SSL tests, eleven transfer regressions,guest_sce_net,guest_tcpandpatched_libraries.The rebase preserves current-main SSL transport and certificate behavior. The production change, lifecycle regression and CMake registration are unchanged from the previous PR head. The documentation conflict preserves all current-main entries and removes exactly the two obsolete ownership clauses.
Validation commands (
buildis the configured full Windows build directory):Conventions has zero errors and one warning about the existing 7,600-character debt entry. Main-sync and whitespace checks pass. The overlap tool lists this PR itself because its hosted head differs from the rebased local branch. Manual review of uncompared shared files confirms that #6039 edits a separate debt entry, #6044 moves existing test paths, #8951 declares a separate export, and #9173 changes separate asymmetric-key cleanup exports. I also checked related #8658 and #9407; none implements context-termination cleanup or adds a functional prerequisite.
Original regression evidence is pinned to base
d073fe6a1da82441d33f4d3da72cc5f7d3496259and fix15afb86473371f57bb2c6fa0d80bd3e4d7d049b8:The same regression failed on that unchanged base with
termination retained orphan connection records. The original five baseline SSL tests and six fixed SSL tests passed three executions each. One initial fixed run failed the existing net transport handshake assertion with a 200 ms receive timeout; an isolated rerun, those repeated checks and six further diagnostic handshakes passed. The timeout and assertion were not changed, and the exact transient cause was not reproduced. These are historical results for the pinned revisions above.The lifecycle regression checks 32 concurrent create/delete/term cycles, separate context ownership and an in-flight verification callback retaining its session and pool until completion. The callback case initializes TLS backend state but performs no wire I/O; the transport tests exercise loopback TLS through the kernel and libSceNet socket tables.
No complete CTest suite, GPU execution or title/gameplay run is claimed. This fixes host registry ownership; it does not establish a PRAGMATA OOM or crash cause.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T19:05:01Z.
This PR does not change docs/dev/debt/libSceSsl.md line 5, which still describes the old
sceSslTerm:sceSslTerm, whose connections returnSSL_ERROR_INVALID_IDfrom then on"sceSslTermleaves the context's connections tosceSslDeleteConnection, which then returnsSSL_ERROR_INVALID_ID"With this PR,
sceSslInit→sceSslCreateConnection→sceSslConnect→sceSslTermerases the connection insidesceSslTerm(Export.cpp#L625). Its session and the pool are freed right there. If a call on that connection is still running, they are freed when that call returns. The new test checks both cases (SslContextLifetime.cpp#L70, #L163). After the merge, the entry would still describe the orphaned records that this PR removes.Of this, only the
SSL_ERROR_INVALID_IDreturns were measured on 13.40 (#7756). The entry already states them earlier in the same line ("every connection call ... on a connection whose context was terminated returnsSSL_ERROR_INVALID_ID"). The "leaves the context's connections tosceSslDeleteConnection" and "keeps the pool alive" parts described how this build was designed, not console results.d569439e1fadded the first one, saying "none of this is verified on the console", ande37137241d(#6828) added the second. CONTRIBUTING.md asks a PR that closes a gap to cut the entry down to what is still open, so I would remove both clauses and keep the rest:Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-20T20:25:54Z.
@nikowolf123 Thanks for catching this. I removed both obsolete ownership clauses from
docs/dev/debt/libSceSsl.mdin 321e6de791, preserving the measuredSSL_ERROR_INVALID_IDbehavior and the rest of the entry as you suggested.This update only changes documentation. The conventions check passes with zero errors and one warning about the existing entry length; the whitespace check also passes.
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T04:48:23Z.
Independent Windows run of this PR (data only).
Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against
mainis clean. I built onlyguest_ssl_context_lifetime_tests;ctest -N -R guest_ssl_context_lifetimelists exactly one test.Results (3 runs each):
guest_ssl_context_lifetimepasses 3/3.core/libs/prx/libSceSsl/Export.cppwith the version from the PR's merge base (d073fe6a1d), then rebuilt. The test fails 3/3 (ctest reports Failed, a non-zero exit). I restored the PR's file afterwards.Not run: other platforms, the other tests in the same family, the full repository CTest, and any title or console run.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:50:19Z.
Review: APPROVED
Checked against main
522e32e3b6and the libSceSsl Export.cpp / libkernel Udp.cpp sources on main:sceSslTermstill only erases the context, andFindConnectionrejects the orphan afterwards, so the leak this fixes is still on main.Sessionholdsshared_ptr<Context>, andEstablishcopies the session under the registry lock, so an operation in progress keeps both alive after Term; the handshake-failure path looks the connection up again by id; and nothing destroyed understate.mutextakes that mutex again. I found no use-after-free, double-free or deadlock.sceSslDeleteConnectiondeclaressessionbefore thelock_guard, so the lastSessionreference is released after unlock. In the new Term,erase_ifdrops idle sessions inside the lock, includingsocketOwner, which can be the last reference to the libkernelSocket(its destructor closes the host socket) if the title already closed the descriptor. Moving the erased sessions into a vector declared before the lock would match DeleteConnection. From reading the code, I don't think it can deadlock; it's only extra work under the global SSL mutex.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.