Skip to content

Commit c52a633

Browse files
authored
[SYCL] Catch exceptions thrown in destructors (#14808)
In the same vein as #14273, this PR prevents exceptions from leaking in additional destructors caught by Coverity. I'd like to draw attention to `device_impl.cpp` however: A comment was left suggesting that exceptions in the `device_impl` destructor be added to the asynchronous exceptions list. Given that devices are usually destroyed during shutdown, adding exceptions to the exceptions list doesn't seem to make sense, as there would be nothing to handle the exceptions anyway. However, if this understanding is incorrect, and I should still add exceptions to an asynchronous exceptions list, please let me know. Thanks!
1 parent 7b74721 commit c52a633

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

sycl/source/detail/device_impl.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,14 @@ device_impl::device_impl(ur_native_handle_t InteropDeviceHandle,
7676
}
7777

7878
device_impl::~device_impl() {
79-
// TODO catch an exception and put it to list of asynchronous exceptions
80-
const PluginPtr &Plugin = getPlugin();
81-
ur_result_t Err = Plugin->call_nocheck(urDeviceRelease, MDevice);
82-
__SYCL_CHECK_OCL_CODE_NO_EXC(Err);
79+
try {
80+
// TODO catch an exception and put it to list of asynchronous exceptions
81+
const PluginPtr &Plugin = getPlugin();
82+
ur_result_t Err = Plugin->call_nocheck(urDeviceRelease, MDevice);
83+
__SYCL_CHECK_OCL_CODE_NO_EXC(Err);
84+
} catch (std::exception &e) {
85+
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in ~device_impl", e);
86+
}
8387
}
8488

8589
bool device_impl::is_affinity_supported(

sycl/source/detail/kernel_program_cache.hpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,14 @@ class KernelProgramCache {
100100
this->State.store(InitialState);
101101
}
102102
~ProgramBuildResult() {
103-
if (Val) {
104-
ur_result_t Err = Plugin->call_nocheck(urProgramRelease, Val);
105-
__SYCL_CHECK_OCL_CODE_NO_EXC(Err);
103+
try {
104+
if (Val) {
105+
ur_result_t Err = Plugin->call_nocheck(urProgramRelease, Val);
106+
__SYCL_CHECK_OCL_CODE_NO_EXC(Err);
107+
}
108+
} catch (std::exception &e) {
109+
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in ~ProgramBuildResult",
110+
e);
106111
}
107112
}
108113
};
@@ -133,9 +138,13 @@ class KernelProgramCache {
133138
Val.first = nullptr;
134139
}
135140
~KernelBuildResult() {
136-
if (Val.first) {
137-
ur_result_t Err = Plugin->call_nocheck(urKernelRelease, Val.first);
138-
__SYCL_CHECK_OCL_CODE_NO_EXC(Err);
141+
try {
142+
if (Val.first) {
143+
ur_result_t Err = Plugin->call_nocheck(urKernelRelease, Val.first);
144+
__SYCL_CHECK_OCL_CODE_NO_EXC(Err);
145+
}
146+
} catch (std::exception &e) {
147+
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in ~KernelBuildResult", e);
139148
}
140149
}
141150
};

0 commit comments

Comments
 (0)