Skip to content

Commit 83d2343

Browse files
committed
fix: fix hook poll bug for curl post request on the same fd
1 parent 6d49ec7 commit 83d2343

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

co_hook_sys_call.cpp

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <netdb.h>
4242

4343
#include <time.h>
44+
#include <map>
4445
#include "co_routine.h"
4546
#include "co_routine_inner.h"
4647
#include "co_routine_specific.h"
@@ -575,15 +576,48 @@ extern int co_poll_inner( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int
575576

576577
int poll(struct pollfd fds[], nfds_t nfds, int timeout)
577578
{
578-
579579
HOOK_SYS_FUNC( poll );
580580

581-
if( !co_is_enable_sys_hook() )
582-
{
583-
return g_sys_poll_func( fds,nfds,timeout );
581+
if (!co_is_enable_sys_hook() || timeout == 0) {
582+
return g_sys_poll_func(fds, nfds, timeout);
583+
}
584+
pollfd *fds_merge = NULL;
585+
nfds_t nfds_merge = 0;
586+
std::map<int, int> m; // fd --> idx
587+
std::map<int, int>::iterator it;
588+
if (nfds > 1) {
589+
fds_merge = (pollfd *)malloc(sizeof(pollfd) * nfds);
590+
for (size_t i = 0; i < nfds; i++) {
591+
if ((it = m.find(fds[i].fd)) == m.end()) {
592+
fds_merge[nfds_merge] = fds[i];
593+
m[fds[i].fd] = nfds_merge;
594+
nfds_merge++;
595+
} else {
596+
int j = it->second;
597+
fds_merge[j].events |= fds[i].events; // merge in j slot
598+
}
599+
}
584600
}
585601

586-
return co_poll_inner( co_get_epoll_ct(),fds,nfds,timeout, g_sys_poll_func);
602+
int ret = 0;
603+
if (nfds_merge == nfds || nfds == 1) {
604+
ret = co_poll_inner(co_get_epoll_ct(), fds, nfds, timeout, g_sys_poll_func);
605+
} else {
606+
ret = co_poll_inner(co_get_epoll_ct(), fds_merge, nfds_merge, timeout,
607+
g_sys_poll_func);
608+
if (ret > 0) {
609+
for (size_t i = 0; i < nfds; i++) {
610+
it = m.find(fds[i].fd);
611+
if (it != m.end()) {
612+
int j = it->second;
613+
fds[i].revents = fds_merge[j].revents & fds[i].events;
614+
}
615+
}
616+
}
617+
}
618+
free(fds_merge);
619+
return ret;
620+
587621

588622
}
589623
int setsockopt(int fd, int level, int option_name,

co_routine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ static unsigned long long GetTickMS()
114114
#endif
115115
}
116116

117+
/* no longer use
117118
static pid_t GetPid()
118119
{
119120
static __thread pid_t pid = 0;
@@ -141,7 +142,6 @@ static pid_t GetPid()
141142
return tid;
142143
143144
}
144-
/*
145145
static pid_t GetPid()
146146
{
147147
char **p = (char**)pthread_self();

0 commit comments

Comments
 (0)