ucx icon indicating copy to clipboard operation
ucx copied to clipboard

Question about what we must not do in err call back

Open huzhijiang opened this issue 2 years ago • 6 comments

https://github.com/openucx/ucx/blob/1c25669488b08081d2ad7d1922a35ae0b428aa5d/examples/ucp_client_server.c#L193

We tried to close ep (ucp_ep_close_nbx()) in this above error call back but encountered some request being freed twice cause assertion in ucp_request_complete_send(). So we have to move close ep out of the err callback just like the example do.

The question is, is this by design that user must not do close ep in the err call back? Is there any documentation to guide the user what they can do and what they must not do in the err callback?

huzhijiang avatar Mar 01 '24 05:03 huzhijiang

@huzhijiang can you pls share the code of the error callback?

yosefe avatar Mar 05 '24 09:03 yosefe

@huzhijiang it's allowed to close an endpoint from error handling, the only specific is once the ep is failed then it does not make sense to post new operations and try "flush" the ep on closing, use UCP_EP_CLOSE_FLAG_FORCE instead.

in gtests you can find and an example https://github.com/openucx/ucx/blob/1c25669488b08081d2ad7d1922a35ae0b428aa5d/test/gtest/ucp/test_ucp_sockaddr.cc#L1994

Also it's not allowed to call ucp_worker_progress from the error handler callback, this rule is common for all UCP callbacks.

evgeny-leksikov avatar Mar 05 '24 09:03 evgeny-leksikov

@huzhijiang, does comment above help?

evgeny-leksikov avatar Mar 18 '24 08:03 evgeny-leksikov

@evgeny-leksikov , Sorry for replying late. Thanks for the answer.

But, actually we do use UCP_EP_CLOSE_FLAG_FORCE when calling ucp_ep_close_nbx(), our code piece is as follows:

memset(&param, 0, sizeof(ucp_request_param_t)); param.op_attr_mask = UCP_OP_ATTR_FIELD_FLAGS; param.flags = UCP_EP_CLOSE_FLAG_FORCE;/*UCP_ERR_HANDLING_MODE_PEER should set*/ close_req = ucp_ep_close_nbx(ep, &param);

We are using ucx-1.12.1(sorry for not mention before).

With some days research we seem found that UCP_EP_CLOSE_FLAG_FORCE is the key to the problem:

  1. Assertion triggered in ucp_rndv_send_rts() when low level link error occured. https://github.com/openucx/ucx/blob/dc9243556ec10178fc9fc2f8be5e17f2bf79cd19/src/ucp/rndv/rndv.c#L393

  2. ucp_do_am_single() -> uct_tcp_ep_am_short() -> uct_tcp_ep_am_short_sendv() -> uct_tcp_ep_am_sendv() -> uct_tcp_ep_handle_send_err() -> uct_tcp_ep_handle_disconnected() -> uct_tcp_ep_set_failed() -> uct_iface_handle_ep_err() ->ucp_worker_iface_error_handler() -> ucp_worker_iface_handle_uct_ep_failure() -> ucp_ep_set_failed() -> ucp_ep_invoke_err_cb(). Thus, all the way to the user registered ep err callback handler.

  3. In user error callback handler, we do ucp_ep_close_nbx with UCP_EP_CLOSE_FLAG_FORCE , which triggers ucp_ep_disconnected() https://github.com/openucx/ucx/blob/dc9243556ec10178fc9fc2f8be5e17f2bf79cd19/src/ucp/core/ucp_ep.c#L1511

  4. Then ucp_ep_disconnected() calls ucp_ep_reqs_purge(ep, UCS_ERR_CANCELED) which eventual mark all messages in proto_reqs queue to be UCP_REQUEST_FLAG_COMPLETED by calling ucp_request_complete() macro. https://github.com/openucx/ucx/blob/dc9243556ec10178fc9fc2f8be5e17f2bf79cd19/src/ucp/core/ucp_ep.c#L1334

  5. After returning from ucp_do_am_single(), ucp_rndv_send_rts() continue with ucp_rndv_send_handle_status_from_pending() which then calls ucp_request_complete() macro again and that is why assertion was triggered.

  6. The assertion is in ucp_request_complete() macro at this line of code: ucs_assert(!((_req)->flags & UCP_REQUEST_FLAG_COMPLETED)); \

This made me think ucp_ep_close_nbx() with UCP_EP_CLOSE_FLAG_FORCE can not be used in ep error callbake handler.

But from the newer code such as ucx-1.15.0, ucp_ep_disconnected() will not call ucp_ep_reqs_purge(ep, UCS_ERR_CANCELED) any more, which invalidates step 4 in my sequence above. Does that means that we can not do ucp_ep_close_nbx() with UCP_EP_CLOSE_FLAG_FORCE in old version but CAN do in newer version?

huzhijiang avatar Mar 19 '24 11:03 huzhijiang

But from the newer code such as ucx-1.15.0, ucp_ep_disconnected() will not call ucp_ep_reqs_purge(ep, UCS_ERR_CANCELED) any more, which invalidates step 4 in my sequence above. Does that means that we can not do ucp_ep_close_nbx() with UCP_EP_CLOSE_FLAG_FORCE in old version but CAN do in newer version?

I would consider this as a bug fix and suggest to upgrade to newer UCX version

evgeny-leksikov avatar Mar 22 '24 11:03 evgeny-leksikov

Hi @evgeny-leksikov , Thanks for the info. Im going to close this issue.

huzhijiang avatar Mar 25 '24 09:03 huzhijiang