@@ -53,9 +53,10 @@ let parameter_count_mismatch_failure func expected received =
53
53
API. response_of_failure Api_errors. message_parameter_count_mismatch
54
54
[func; expected; received]
55
55
56
- (* * WARNING: the context is destroyed when execution is finished if the task is not forwarded, in database and not called asynchronous. *)
57
- let exec_with_context ~__context ~need_complete ?marshaller ?f_forward
58
- ?(called_async = false ) ?quiet f =
56
+ (* * WARNING: DOES NOT DESTROY the context when execution is finished. The
57
+ caller must destroy it *)
58
+ let exec_with_context ~__context ~need_complete ?marshaller ?f_forward ?quiet f
59
+ =
59
60
(* Execute fn f in specified __context, marshalling result with "marshaller" *)
60
61
let exec () =
61
62
(* NB:
@@ -95,23 +96,15 @@ let exec_with_context ~__context ~need_complete ?marshaller ?f_forward
95
96
if need_complete then TaskHelper. failed ~__context e ;
96
97
raise e
97
98
in
98
- Locking_helpers.Thread_state. with_named_thread
99
- (TaskHelper. get_name ~__context) (Context. get_task_id __context) (fun () ->
100
- let client = Context. get_client __context in
101
- Debug. with_thread_associated ?client ?quiet
102
- (Context. string_of_task __context)
103
- (fun () ->
104
- (* CP-982: promote tracking debug line to info status *)
105
- if called_async then
106
- info " spawning a new thread to handle the current task%s"
107
- (Context. trackid ~with_brackets: true ~prefix: " " __context) ;
108
- Xapi_stdext_pervasives.Pervasiveext. finally exec (fun () ->
109
- if not called_async then Context. destroy __context
110
- (* else debug "nothing more to process for this thread" *)
111
- )
112
- )
113
- ()
114
- )
99
+ let @ () =
100
+ Locking_helpers.Thread_state. with_named_thread
101
+ (TaskHelper. get_name ~__context)
102
+ (Context. get_task_id __context)
103
+ in
104
+ let client = Context. get_client __context in
105
+ Debug. with_thread_associated ?client ?quiet
106
+ (Context. string_of_task __context)
107
+ exec ()
115
108
116
109
let dispatch_exn_wrapper f =
117
110
try f ()
@@ -168,18 +161,22 @@ let do_dispatch ?session_id ?forward_op ?self:_ supports_async called_fn_name
168
161
169
162
let sync () =
170
163
let need_complete = not (Context. forwarded_task __context) in
171
- exec_with_context ~__context ~need_complete ~called_async
172
- ?f_forward:forward_op ~marshaller op_fn
164
+ let @ __context = Context. finally_destroy_context ~__context in
165
+ exec_with_context ~__context ~need_complete ?f_forward:forward_op
166
+ ~marshaller op_fn
173
167
|> marshaller
174
168
|> Rpc. success
175
169
in
170
+
176
171
let async ~need_complete =
177
172
(* Fork thread in which to execute async call *)
173
+ info " spawning a new thread to handle the current task%s"
174
+ (Context. trackid ~with_brackets: true ~prefix: " " __context) ;
178
175
ignore
179
176
(Thread. create
180
177
(fun () ->
181
- exec_with_context ~__context ~need_complete ~called_async
182
- ?f_forward:forward_op ~marshaller op_fn
178
+ exec_with_context ~__context ~need_complete ?f_forward:forward_op
179
+ ~marshaller op_fn
183
180
)
184
181
()
185
182
) ;
@@ -200,26 +197,27 @@ let do_dispatch ?session_id ?forward_op ?self:_ supports_async called_fn_name
200
197
(* in the following functions, it is our responsibility to complete any tasks we create *)
201
198
let exec_with_new_task ?http_other_config ?quiet ?subtask_of ?session_id
202
199
?task_in_database ?task_description ?origin task_name f =
203
- exec_with_context ?quiet
204
- ~__context:
205
- (Context. make ?http_other_config ?quiet ?subtask_of ?session_id
206
- ?task_in_database ?task_description ?origin task_name
207
- ) ~need_complete: true (fun ~__context -> f __context
200
+ let @ __context =
201
+ Context. with_context ?http_other_config ?quiet ?subtask_of ?session_id
202
+ ?task_in_database ?task_description ?origin task_name
203
+ in
204
+ exec_with_context ~__context ~need_complete: true (fun ~__context ->
205
+ f __context
208
206
)
209
207
210
208
let exec_with_forwarded_task ?http_other_config ?session_id ?origin task_id f =
211
- exec_with_context
212
- ~__context:
213
- ( Context. from_forwarded_task ?http_other_config ?session_id ?origin
214
- task_id
215
- ) ~need_complete: true ( fun ~ __context -> f __context
209
+ let @ __context =
210
+ Context. with_forwarded_task ?http_other_config ?session_id ?origin task_id
211
+ in
212
+ exec_with_context ~__context ~need_complete: true ( fun ~ __context ->
213
+ f __context
216
214
)
217
215
218
216
let exec_with_subtask ~__context ?task_in_database task_name f =
219
- let subcontext =
220
- Context. make_subcontext ~__context ?task_in_database task_name
217
+ let @ __context =
218
+ Context. with_subcontext ~__context ?task_in_database task_name
221
219
in
222
- exec_with_context ~__context:subcontext ~need_complete: true f
220
+ exec_with_context ~__context ~need_complete: true f
223
221
224
222
let forward_extension ~__context rbac call =
225
223
rbac __context (fun () -> Xapi_extensions. call_extension call)
0 commit comments