Skip to content

Commit e04ea2d

Browse files
committed
Better error handling
1 parent 8ba41f4 commit e04ea2d

1 file changed

Lines changed: 53 additions & 6 deletions

File tree

‎coroio/socket.hpp‎

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ class TSocketBase: public TSocketBase<void> {
128128
/**
129129
* @brief Asynchronously reads data from the socket into the provided buffer.
130130
*
131-
* When awaited, this method suspends the current coroutine until the read operation
132-
* completes and then returns the number of bytes read.
131+
* Semantics: returns bytes read (>0), 0 on closure, and a negative value
132+
* (retry hint) for transient errors (e.g. EINTR/EAGAIN/EINPROGRESS). Non-retryable
133+
* errors throw std::system_error.
133134
*
134135
* @param buf Pointer to the destination buffer.
135136
* @param size The number of bytes to read.
@@ -150,8 +151,9 @@ class TSocketBase: public TSocketBase<void> {
150151
/**
151152
* @brief Forces a read operation on the next event loop iteration.
152153
*
153-
* Similar to @ref ReadSome(), but this variant ensures that the read is deferred
154-
* to the next iteration of the event loop.
154+
* Semantics: returns bytes read (>0), 0 on closure, and a negative value
155+
* (retry hint) for transient errors (e.g. EINTR/EAGAIN/EINPROGRESS). Non-retryable
156+
* errors throw std::system_error.
155157
*
156158
* @param buf Pointer to the buffer where data will be stored.
157159
* @param size Number of bytes to read.
@@ -176,8 +178,9 @@ class TSocketBase: public TSocketBase<void> {
176178
/**
177179
* @brief Asynchronously writes data from the provided buffer to the socket.
178180
*
179-
* This method suspends the current coroutine until the data is written, and then returns
180-
* the number of bytes successfully written.
181+
* Semantics: returns bytes written (>0), 0 if nothing written, and a negative value
182+
* (retry hint) for transient errors (e.g. EINTR/EAGAIN/EINPROGRESS). Non-retryable
183+
* errors throw std::system_error.
181184
*
182185
* @param buf Pointer to the data to be written.
183186
* @param size The number of bytes to write.
@@ -659,6 +662,17 @@ class TPollerDrivenSocket: public TSocket
659662
auto await_resume() {
660663
auto ret = poller->Result();
661664
if (ret < 0) {
665+
#ifdef _WIN32
666+
int err = -ret;
667+
if (err == WSAEWOULDBLOCK || err == WSAEINTR || err == WSAEINPROGRESS) {
668+
return ret; // retry hint
669+
}
670+
#else
671+
int err = -ret;
672+
if (err == EINTR || err == EAGAIN || err == EINPROGRESS) {
673+
return ret; // retry hint
674+
}
675+
#endif
662676
throw std::system_error(-ret, std::generic_category());
663677
}
664678
return ret;
@@ -694,6 +708,17 @@ class TPollerDrivenSocket: public TSocket
694708
auto await_resume() {
695709
auto ret = poller->Result();
696710
if (ret < 0) {
711+
#ifdef _WIN32
712+
int err = -ret;
713+
if (err == WSAEWOULDBLOCK || err == WSAEINTR || err == WSAEINPROGRESS) {
714+
return ret; // retry hint
715+
}
716+
#else
717+
int err = -ret;
718+
if (err == EINTR || err == EAGAIN || err == EINPROGRESS) {
719+
return ret; // retry hint
720+
}
721+
#endif
697722
throw std::system_error(-ret, std::generic_category());
698723
}
699724
return ret;
@@ -776,6 +801,17 @@ class TPollerDrivenFileHandle: public TFileHandle
776801
auto await_resume() {
777802
auto ret = poller->Result();
778803
if (ret < 0) {
804+
#ifdef _WIN32
805+
int err = -ret;
806+
if (err == WSAEWOULDBLOCK || err == WSAEINTR || err == WSAEINPROGRESS) {
807+
return ret; // retry hint
808+
}
809+
#else
810+
int err = -ret;
811+
if (err == EINTR || err == EAGAIN || err == EINPROGRESS) {
812+
return ret; // retry hint
813+
}
814+
#endif
779815
throw std::system_error(-ret, std::generic_category());
780816
}
781817
return ret;
@@ -811,6 +847,17 @@ class TPollerDrivenFileHandle: public TFileHandle
811847
auto await_resume() {
812848
auto ret = poller->Result();
813849
if (ret < 0) {
850+
#ifdef _WIN32
851+
int err = -ret;
852+
if (err == WSAEWOULDBLOCK || err == WSAEINTR || err == WSAEINPROGRESS) {
853+
return ret; // retry hint
854+
}
855+
#else
856+
int err = -ret;
857+
if (err == EINTR || err == EAGAIN || err == EINPROGRESS) {
858+
return ret; // retry hint
859+
}
860+
#endif
814861
throw std::system_error(-ret, std::generic_category());
815862
}
816863
return ret;

0 commit comments

Comments
 (0)