Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose tcp_read/ssl_read errors #713

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/colony/lua_tm.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,14 @@ static int l_tm_tcp_read (lua_State* L)
uint8_t buf[512];
size_t buf_len = sizeof(buf);
int err = tm_tcp_read(socket, buf, &buf_len);
(void) err;

if (err != 0) {
lua_pushstring(L, "tcp read error");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See ssl read for a better error formatting strategy

lua_error(L);
} else {
colony_pushbuffer(L, buf, buf_len);
}

colony_pushbuffer(L, buf, buf_len);
return 1;
}

Expand Down Expand Up @@ -427,9 +432,13 @@ static int l_tm_ssl_read (lua_State* L)
uint8_t buf[20000];
size_t buf_len = sizeof(buf);
int err = tm_ssl_read(session, buf, &buf_len);
(void) err;
if (err != 0) {
lua_pushstring(L, "ssl read error");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

char strbuffer[100];
ssize_t len = (ssize_t) snprintf(strbuffer, sizeof(strbuffer), "SSL read error: %d", err);
if (len > 0) {
  lua_pushlstring(L, strbuffer, len);
} else {
  lua_pushstring(L, "SSL read error: [unavailable]");
}

lua_error(L);
} else {
colony_pushbuffer(L, buf, buf_len);
}

colony_pushbuffer(L, buf, buf_len);
return 1;
}

Expand Down
20 changes: 15 additions & 5 deletions src/colony/modules/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,11 @@ TCPSocket.prototype.connect = function (/*options | [port], [host], [cb]*/) {
var closeRet = tm.tcp_close(self.socket); // returns -57 if socket is already closed
if (closeRet < 0 && closeRet != -tm.ENOTCONN){
// couldn't close socket, throw an error
// failed to connect, stay silent
console.log("Failed to close socket", self.socket);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the opposite of staying silent

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But yeah, should we have internal code be emitting console.logs if it's just going to throw the error immediately?

self.emit('error', new Error('ENOENT Cannot close socket ' + self.socket + ' Got: err'+closeRet));
self.destroy();
return self.__close(false);
return self.__close();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be true then? Rather than relying on fuzzy JS boolean conversion semantics.

}

if (retries > 3) {
Expand Down Expand Up @@ -425,9 +427,17 @@ TCPSocket.prototype.__readSocket = function(restartTimeout) {
var arr = [], flag = 0;
while (self.socket != null && (flag = tm.tcp_readable(self.socket)) > 0) {
if (self._ssl) {
var data = tm.ssl_read(self._ssl);
try {
var data = tm.ssl_read(self._ssl);
} catch(e){
self.emit("error", e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.emit("error", typeof e == 'string' ? new Error(e) : e);

}
} else {
var data = tm.tcp_read(self.socket);
try {
var data = tm.tcp_read(self.socket);
} catch(e){
self.emit("error", e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here self.emit("error", typeof e == 'string' ? new Error(e) : e);

}
}
if (!data || data.length == 0) {
break;
Expand Down Expand Up @@ -463,11 +473,11 @@ TCPSocket.prototype.__close = function (tryToClose) {
function closeSocket(){
if (self.socket === null) return;
var ret = tm.tcp_close(self.socket);

if (ret < 0 && ret != -tm.ENOTCONN) { // -57 is inactive, socket has already been closed
if (retries > 3) {
// tried 3 times and couldn't close, error out
self.emit('error', new Error('ENOENT Cannot close socket ' + self.socket + ' Got: err'+ret));
// removing this error for now, nothing for the user to do if this error occurs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this socket error occurs, what is going to happen downstream? Remove the commented out line underneath this and explain more what the reprecussion of not handling this error looks like.

// self.emit('error', new Error('ENOENT Cannot close socket ' + self.socket + ' Got: err'+ret));
self.emit('close');
} else {
retries++;
Expand Down
1 change: 1 addition & 0 deletions src/tm_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ int tm_ssl_read (tm_ssl_session_t ssl, uint8_t *buf, size_t *buf_len)
uint8_t *read_buf;
int res = ssl_read(ssl, &read_buf);
if (res < 0) {
TM_DEBUG("ssl read res %d", res);
*buf_len = 0;
return res;
} else {
Expand Down