Skip to content

Commit 66f0e1e

Browse files
committed
FIX | memory leak
1 parent 58aa9ac commit 66f0e1e

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/worker/ManapiTcp.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -703,12 +703,13 @@ void manapi::net::worker::TCP::read_stop_(const shared_conn &conn, tcp_connectio
703703
int manapi::net::worker::TCP::flush_write_(const worker::shared_conn &connection, bool flush) MANAPIHTTP_NOEXCEPT {
704704
auto conn = connection->as<tcp_connection_t>();
705705

706+
if (!flush) {
707+
flush = (conn->flags & CONN_SEND_END);
708+
}
709+
706710
try {
707-
if (conn->top && conn->top->cur_send_size) {
708-
//std::cout << "flush " << flush << " "<<(bool)conn->top->cur_send_size << " " << (bool)conn->top->send.deque << "\n";
709-
while (conn->top->cur_send_size && ((conn->top->cur_send_size >= this->config_->max_merge_buffer_stack)
710-
//|| ((conn->top->cur_send_size == this->config_->max_merge_buffer_stack) && (conn->top->send.last_deque->buffer.size() == conn->top->send.deque_cursor))
711-
|| (flush))) {
711+
if (conn->top) {
712+
while (conn->top->cur_send_size && ((conn->top->cur_send_size >= this->config_->max_merge_buffer_stack) || (flush))) {
712713
#ifdef _MSC_VER
713714
ev::buff_t *s = static_cast<ev::buff_t*>(alloca(sizeof (ev::buff_t) * conn->top->cur_send_size));
714715
#else
@@ -818,6 +819,9 @@ int manapi::net::worker::TCP::flush_write_(const worker::shared_conn &connection
818819
return;
819820
}
820821

822+
manapi_log_trace(manapi::debug::LOG_TRACE_LOW, "TCP:packets(%d) were sent %p. now=%d",
823+
nbuff, connection.get(), conn->top->send_size);
824+
821825
if ((conn->flags & (ev::WRITE|ev::DISCONNECT)) == ev::WRITE && conn->ev_callback) {
822826
if (base::call_user_callback(&conn->ev_callback, connection, ev::WRITE, nullptr, 0, nullptr)) {
823827
conn->worker->close_connection(connection, CLOSE_CONN_ERR);
@@ -835,9 +839,9 @@ int manapi::net::worker::TCP::flush_write_(const worker::shared_conn &connection
835839
return;
836840
}
837841

838-
if (conn->flags & CONN_SEND_END && conn->flags & CONN_RECV_END) {
839-
manapi_log_trace(manapi::debug::LOG_TRACE_LOW, "TCP:packets were sent after closing conn %p",
840-
connection.get());
842+
if ((conn->flags & CONN_SEND_END) && (conn->flags & CONN_RECV_END)) {
843+
manapi_log_trace(manapi::debug::LOG_TRACE_LOW, "TCP:packets(%d) were sent after closing conn %p. now=%d",
844+
nbuff, connection.get(), conn->top->send_size);
841845
conn->worker->close_connection(connection, CLOSE_CONN_FINISHED);
842846
return;
843847
}

src/worker/ManapiTlsOverTcp.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,19 @@ ssize_t manapi::net::worker::TLS::sync_write_ex(const shared_conn &conn, ev::buf
175175
rhs = this->ssl_write_(connection->ssl, buffer + current,
176176
static_cast<int>(cursor - current));
177177

178-
if (rhs > 0)
178+
if (rhs > 0) {
179179
current += rhs;
180+
181+
if (int erhs = this->ssl_bio_flush_write_(conn, connection, maxcnt, false)) {
182+
if (erhs == CONN_IO_WANT_WRITE) {
183+
if (this->flush_write_(conn, true))
184+
return CONN_IO_ERROR;
185+
return total + current;
186+
}
187+
188+
return CONN_IO_ERROR;
189+
}
190+
}
180191
else {
181192
int err = this->ssl_get_error_(connection->ssl, rhs);
182193

@@ -253,7 +264,7 @@ ssize_t manapi::net::worker::TLS::sync_write(const shared_conn &conn, ev::buff_t
253264
if (!size)
254265
return size;
255266

256-
return this->sync_write_ex(conn, buff, nbuff, size, finish, static_cast<int>(this->config_->max_buffer_stack));
267+
return this->sync_write_ex(conn, buff, nbuff, size, finish, this->config_->max_buffer_stack);
257268
}
258269

259270
int manapi::net::worker::TLS::event_flags(const shared_conn & conn, int flags) MANAPIHTTP_NOEXCEPT {
@@ -611,20 +622,27 @@ int manapi::net::worker::TLS::conn_after_write(const worker::shared_conn &conn)
611622
return rhs;
612623
}
613624

614-
auto const err = this->ssl_bio_flush_write_(conn, conn->as<tls_connection_t>(),
625+
auto const m = conn->as<tls_connection_t>();
626+
627+
auto const err = this->ssl_bio_flush_write_(conn, m,
615628
this->config_->max_buffer_stack, false);
616629

617630
if (err) {
618631
if (err == CONN_IO_WANT_WRITE) {
619632
if (this->flush_write_(conn, true)) {
620-
this->close_connection(conn, CLOSE_CONN_ERR);
621-
622633
return ERR_UNAVAILABLE;
623634
}
635+
624636
return ERR_OK;
625637
}
626638

627-
this->close_connection(conn, CLOSE_CONN_ERR);
639+
return ERR_UNKNOWN;
640+
}
641+
642+
if (m->flags & CONN_TLS_SHUTDOWN || !ssl_is_init_fininshed_(m->ssl)) {
643+
if (this->flush_write_(conn, true)) {
644+
return ERR_UNAVAILABLE;
645+
}
628646
}
629647

630648
return ERR_OK;

0 commit comments

Comments
 (0)