You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2020/11/23 21:21:33 UTC

[GitHub] [incubator-tvm] tqchen commented on a change in pull request #6953: Add retry to sockets on EINTR error

tqchen commented on a change in pull request #6953:
URL: https://github.com/apache/incubator-tvm/pull/6953#discussion_r528996912



##########
File path: src/support/socket.h
##########
@@ -68,6 +68,28 @@ static inline int poll(struct pollfd* pfd, int nfds, int timeout) {
 
 namespace tvm {
 namespace support {
+
+/*!
+ * \brief Call a function and retry if an EINTR error is encountered.
+ * \param f The function to retry.
+ * \param error_value The error value returned by the call on retry failure.
+ * \return The return code returned by function f or error_value on retry failure.
+ */
+template <typename T>
+T retry_call(std::function<T()> f, T error_value) {
+  errno = 0;
+  T rc = error_value;
+  for (size_t retry = 0; retry < 8; ++retry) {
+    rc = f();
+    if (errno == EINTR) {
+      rc = error_value;
+    } else {
+      break;

Review comment:
       directly return error_value here

##########
File path: src/support/socket.h
##########
@@ -68,6 +68,28 @@ static inline int poll(struct pollfd* pfd, int nfds, int timeout) {
 
 namespace tvm {
 namespace support {
+
+/*!
+ * \brief Call a function and retry if an EINTR error is encountered.
+ * \param f The function to retry.
+ * \param error_value The error value returned by the call on retry failure.
+ * \return The return code returned by function f or error_value on retry failure.
+ */
+template <typename T>
+T retry_call(std::function<T()> f, T error_value) {
+  errno = 0;
+  T rc = error_value;
+  for (size_t retry = 0; retry < 8; ++retry) {

Review comment:
       8 seems to be a magic number, need a constant

##########
File path: src/support/socket.h
##########
@@ -68,6 +68,28 @@ static inline int poll(struct pollfd* pfd, int nfds, int timeout) {
 
 namespace tvm {
 namespace support {
+
+/*!
+ * \brief Call a function and retry if an EINTR error is encountered.
+ * \param f The function to retry.
+ * \param error_value The error value returned by the call on retry failure.
+ * \return The return code returned by function f or error_value on retry failure.
+ */
+template <typename T>
+T retry_call(std::function<T()> f, T error_value) {

Review comment:
       Prefer not using std::function, because that means construction of std::function in cases where it can be inlined. Instead, use the following signature so f can be inlined.
   
   ```c++
   template <typename F>
   T RetryCallWhenEINTR(F f, T error_value);
   ```

##########
File path: src/support/socket.h
##########
@@ -68,6 +68,28 @@ static inline int poll(struct pollfd* pfd, int nfds, int timeout) {
 
 namespace tvm {
 namespace support {
+
+/*!
+ * \brief Call a function and retry if an EINTR error is encountered.
+ * \param f The function to retry.
+ * \param error_value The error value returned by the call on retry failure.
+ * \return The return code returned by function f or error_value on retry failure.
+ */
+template <typename T>
+T retry_call(std::function<T()> f, T error_value) {

Review comment:
       inline this function, as it is in header file.
   
   Use CamelCase 

##########
File path: src/support/socket.h
##########
@@ -68,6 +68,28 @@ static inline int poll(struct pollfd* pfd, int nfds, int timeout) {
 
 namespace tvm {
 namespace support {
+
+/*!
+ * \brief Call a function and retry if an EINTR error is encountered.
+ * \param f The function to retry.
+ * \param error_value The error value returned by the call on retry failure.
+ * \return The return code returned by function f or error_value on retry failure.
+ */
+template <typename T>
+T retry_call(std::function<T()> f, T error_value) {
+  errno = 0;
+  T rc = error_value;
+  for (size_t retry = 0; retry < 8; ++retry) {

Review comment:
       Consider start with a first call to f and return, then the less freqeuent path of retry. The compiler can pick that up as opposed to enter a loop in most of the time.

##########
File path: src/support/socket.h
##########
@@ -68,6 +68,28 @@ static inline int poll(struct pollfd* pfd, int nfds, int timeout) {
 
 namespace tvm {
 namespace support {
+
+/*!
+ * \brief Call a function and retry if an EINTR error is encountered.
+ * \param f The function to retry.
+ * \param error_value The error value returned by the call on retry failure.
+ * \return The return code returned by function f or error_value on retry failure.
+ */
+template <typename T>
+T retry_call(std::function<T()> f, T error_value) {
+  errno = 0;
+  T rc = error_value;

Review comment:
       error_value is not needed if you get the rc in the first call

##########
File path: src/support/socket.h
##########
@@ -68,6 +68,28 @@ static inline int poll(struct pollfd* pfd, int nfds, int timeout) {
 
 namespace tvm {
 namespace support {
+
+/*!
+ * \brief Call a function and retry if an EINTR error is encountered.
+ * \param f The function to retry.
+ * \param error_value The error value returned by the call on retry failure.
+ * \return The return code returned by function f or error_value on retry failure.
+ */
+template <typename T>
+T retry_call(std::function<T()> f, T error_value) {
+  errno = 0;

Review comment:
       not sure if it is the best pratice to set errno, instead, check rc first, then check errno




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org