{"record":{"id":"114238f172cee364","repo":"Orbmu2k/nvidiaProfileInspector","slug":"drs-destroysession","errorCode":null,"errorMessage":"DRS_DestroySession","messagePattern":"DRS_DestroySession","errorType":"exception","errorClass":"NvapiException","httpStatus":null,"severity":"warning","filePath":"nvidiaProfileInspector/Common/DrsSessionScope.cs","lineNumber":86,"sourceCode":"            if (csRes != NvAPI_Status.NVAPI_OK)\n                throw new NvapiException(\"DRS_CreateSession\", csRes);\n\n            try\n            {\n                if (!preventLoadSettings)\n                {\n                    var nvRes = nvw.Instance.DRS_LoadSettings(hSession);\n                    if (nvRes != NvAPI_Status.NVAPI_OK)\n                        throw new NvapiException(\"DRS_LoadSettings\", nvRes);\n                }\n\n                return action(hSession);\n            }\n            finally\n            {\n                var nvRes = nvw.Instance.DRS_DestroySession(hSession);\n                if (nvRes != NvAPI_Status.NVAPI_OK)\n                    throw new NvapiException(\"DRS_DestroySession\", nvRes);\n            }\n\n        }\n\n\n    }\n}\n","sourceCodeStart":68,"sourceCodeEnd":94,"githubUrl":"https://github.com/Orbmu2k/nvidiaProfileInspector/blob/2f50c388b3a4d661cade66b32746bec096d1eee1/nvidiaProfileInspector/Common/DrsSessionScope.cs#L68-L94","documentation":"NvapiException(\"DRS_DestroySession\", status) is thrown from the finally block of NonGlobalDrsSession when DRS_DestroySession returns non-OK while cleaning up the per-operation session. Because it is thrown from finally, it can mask an exception thrown by the wrapped action.","triggerScenarios":"Any scoped DRS operation (NonGlobalDrsSession callers) where the action completes or throws, then nvw.Instance.DRS_DestroySession(hSession) returns non-NVAPI_OK at DrsSessionScope.cs:86.","commonSituations":"Driver API state corrupted earlier in the operation (the real failure being masked); session already invalid because an earlier call failed; driver crash/reset mid-operation; rapid concurrent session teardown on the same driver state.","solutions":["Inspect any inner/original exception — the destroy failure is often secondary to the real error","Ensure all handles derived from the session (profiles, applications) were released before destroy","Avoid sharing session handles across threads; NonGlobalDrsSession sessions are not thread-safe for concurrent use","Update the NVIDIA driver if destroy consistently fails","Treat destroy failures as non-fatal where possible by wrapping cleanup in its own try-catch"],"exampleFix":"// before\nfinally\n{\n    var nvRes = nvw.Instance.DRS_DestroySession(hSession);\n    if (nvRes != NvAPI_Status.NVAPI_OK)\n        throw new NvapiException(\"DRS_DestroySession\", nvRes);\n}\n// after\nfinally\n{\n    var nvRes = nvw.Instance.DRS_DestroySession(hSession);\n    if (nvRes != NvAPI_Status.NVAPI_OK)\n        Log.Warn($\"DRS_DestroySession failed: {nvRes}\"); // don't mask action exceptions\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    NonGlobalDrsSession(h => DoWork(h));\n}\ncatch (NvapiException ex)\n{\n    // Inspect ex — destroy failures in finally can mask the real error\n    Log.Error($\"DRS operation failed: {ex.Message} {ex.Status}\");\n}","preventionTips":["Release all handles derived from the session before it is destroyed","Don't use a session from multiple threads concurrently","Treat destroy-status failures as secondary; find the root cause in the action's exception","Log destroy failures instead of rethrowing from finally to avoid masking"],"tags":["nvapi","drs","session-cleanup","driver-settings"],"backgroundTag":"api-error-response","analyzedSha":"2f50c388b3a4d661cade66b32746bec096d1eee1","analyzedAt":"2026-09-15T05:23:52.218Z","contentChangedAt":"2026-09-15T05:23:52.218Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}