Skip to content

Commit

Permalink
Fix bug with closing xray socket too early (#124)
Browse files Browse the repository at this point in the history
* Fix bug with closing xray socket too early

* Fix unit tests
  • Loading branch information
DarcyRaynerDD authored Nov 10, 2020
1 parent 96a0f5a commit acd03f2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/trace/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ jest.mock("dgram", () => {
return {
createSocket: () => {
return {
send: (message: string) => {
send: (
message: string,
start: number,
length: number,
port: number,
address: string,
callback: (error: string | undefined, bytes: number) => void,
) => {
sentSegment = message;
callback(undefined, 1);
},
close: () => {
closedSocket = true;
Expand Down Expand Up @@ -475,6 +483,7 @@ describe("extractTraceContext", () => {
extractTraceContext(stepFunctionEvent);

expect(sentSegment instanceof Buffer).toBeTruthy();

expect(closedSocket).toBeTruthy();

const sentMessage = sentSegment.toString();
Expand Down
5 changes: 2 additions & 3 deletions src/trace/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,12 @@ export function sendXraySubsegment(segment: string) {
client = createSocket("udp4");
// Send segment asynchronously to xray daemon
client.send(message, 0, message.length, port, address, (error, bytes) => {
client?.close();
logDebug(`Xray daemon received metadata payload`, { error, bytes });
});
} catch (error) {
logDebug("Error occurred submitting to xray daemon", { error });
} finally {
// Cleanup socket
client?.close();
logDebug("Error occurred submitting to xray daemon", { error });
}
}

Expand Down

0 comments on commit acd03f2

Please sign in to comment.