Skip to content

Commit

Permalink
made the server temp file a one-off and explicitly delete it (#862)
Browse files Browse the repository at this point in the history
  • Loading branch information
EagleoutIce committed Jul 5, 2024
2 parents 1606c4f + 1389caf commit e955317
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/cli/repl/server/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ export class FlowRServerConnection {
pipeline: PipelineExecutor<typeof DEFAULT_SLICING_PIPELINE>
}>()

private tempFile: tmp.FileResult | undefined

// we do not have to ensure synchronized shell-access as we are always running synchronized
constructor(socket: Socket, name: string, shell: RShell) {
this.socket = socket
Expand Down Expand Up @@ -82,7 +80,7 @@ export class FlowRServerConnection {
}
switch(request.message.type) {
case 'request-file-analysis':
this.handleFileAnalysisRequest(request.message as FileAnalysisRequestMessage)
void this.handleFileAnalysisRequest(request.message as FileAnalysisRequestMessage)
break
case 'request-slice':
this.handleSliceRequest(request.message as SliceRequestMessage)
Expand All @@ -101,7 +99,7 @@ export class FlowRServerConnection {
}
}

private handleFileAnalysisRequest(base: FileAnalysisRequestMessage) {
private async handleFileAnalysisRequest(base: FileAnalysisRequestMessage) {
const requestResult = validateMessage(base, requestAnalysisMessage)
if(requestResult.type === 'error') {
answerForValidationError(this.socket, requestResult, base.id)
Expand All @@ -113,9 +111,11 @@ export class FlowRServerConnection {
if(message.filetoken && this.fileMap.has(message.filetoken)) {
this.logger.warn(`File token ${message.filetoken} already exists. Overwriting.`)
}
const slicer = this.createPipelineExecutorForRequest(message)

void slicer.allRemainingSteps(false).then(async results => await this.sendFileAnalysisResponse(results, message))
const tempFile = tmp.fileSync({ postfix: '.R' })
const slicer = this.createPipelineExecutorForRequest(message, tempFile.name)

await slicer.allRemainingSteps(false).then(async results => await this.sendFileAnalysisResponse(results, message))
.catch(e => {
this.logger.error(`[${this.name}] Error while analyzing file ${message.filename ?? 'unknown file'}: ${String(e)}`)
sendMessage<FlowrErrorMessage>(this.socket, {
Expand All @@ -125,6 +125,9 @@ export class FlowRServerConnection {
reason: `Error while analyzing file ${message.filename ?? 'unknown file'}: ${String(e)}`
})
})

// this is a weird function name that means "I am a callback that removes a file" - so this deletes the file
tempFile.removeCallback()
}

private async sendFileAnalysisResponse(results: Partial<PipelineOutput<typeof DEFAULT_SLICING_PIPELINE>>, message: FileAnalysisRequestMessage): Promise<void> {
Expand Down Expand Up @@ -159,13 +162,12 @@ export class FlowRServerConnection {
}
}

private createPipelineExecutorForRequest(message: FileAnalysisRequestMessage) {
private createPipelineExecutorForRequest(message: FileAnalysisRequestMessage, tempFile: string) {
let request: RParseRequest
if(message.content !== undefined){
// we store the code in a temporary file in case it's too big for the shell to handle
const temp = this.getTempFile()
fs.writeFileSync(temp.name, message.content ?? '')
request = { request: 'file', content: temp.name }
fs.writeFileSync(tempFile, message.content ?? '')
request = { request: 'file', content: tempFile }
} else {
request = { request: 'file', content: message.filepath as string }
}
Expand All @@ -185,14 +187,6 @@ export class FlowRServerConnection {
return slicer
}

private getTempFile(): tmp.FileResult {
if(!this.tempFile) {
this.tempFile = tmp.fileSync({ postfix: '.R', keep: false })
this.socket.on('close', () => this.tempFile?.removeCallback())
}
return this.tempFile
}

private handleSliceRequest(base: SliceRequestMessage) {
const requestResult = validateMessage(base, requestSliceMessage)
if(requestResult.type === 'error') {
Expand Down

2 comments on commit e955317

@github-actions
Copy link

Choose a reason for hiding this comment

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

"artificial" Benchmark Suite

Benchmark suite Current: e955317 Previous: d16b9a2 Ratio
Retrieve AST from R code 242.5159738181818 ms (101.35341528869894) 241.68802936363636 ms (102.8761180575731) 1.00
Normalize R AST 31.297347681818184 ms (60.71540954383679) 31.739752136363638 ms (62.50756200538419) 0.99
Produce dataflow information 55.51897009090909 ms (133.8645148477302) 53.60407036363637 ms (129.65284550123417) 1.04
Total per-file 1330.2552245454544 ms (3312.794132572792) 1269.491520090909 ms (3081.8881842692626) 1.05
Static slicing 1.1783078551009216 ms (1.0356507622351374) 1.1992957765588008 ms (1.0956190505793448) 0.98
Reconstruct code 0.4195224674708633 ms (0.25089561762346574) 0.40823540912587025 ms (0.23279531342826934) 1.03
Total per-slice 1.615581916673621 ms (1.1084954415442283) 1.6255373838885934 ms (1.1638162453002103) 0.99
failed to reconstruct/re-parse 0 # 0 # 1
times hit threshold 0 # 0 # 1
reduction (characters) 0.7974469447714406 # 0.7974469447714406 # 1
reduction (normalized tokens) 0.774073695901592 # 0.774073695901592 # 1
memory (df-graph) 147.58589311079547 KiB (359.2574768951678) 147.58589311079547 KiB (359.2574768951678) 1

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

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

"social-science" Benchmark Suite

Benchmark suite Current: e955317 Previous: d16b9a2 Ratio
Retrieve AST from R code 246.04560519999998 ms (48.48007134211545) 247.62148213999998 ms (45.06237178263203) 0.99
Normalize R AST 31.87852776 ms (25.781599200007758) 31.95647656 ms (25.60791349140293) 1.00
Produce dataflow information 80.04564744 ms (90.27744550891569) 81.06377198 ms (92.78105042956382) 0.99
Total per-file 2228.10956816 ms (3823.7042709419184) 2253.7859776799996 ms (3816.1054094291667) 0.99
Static slicing 4.3118198767643054 ms (8.005738739593546) 4.320062977207287 ms (7.979427803781336) 1.00
Reconstruct code 0.38711299015565137 ms (0.1988607310863943) 0.4180827296166881 ms (0.21129610863943676) 0.93
Total per-slice 4.707821882546963 ms (8.08391138355507) 4.7471979325717735 ms (8.060206129507852) 0.99
failed to reconstruct/re-parse 0 # 0 # 1
times hit threshold 0 # 0 # 1
reduction (characters) 0.9250085598882571 # 0.9250085598882571 # 1
reduction (normalized tokens) 0.8936602769604 # 0.8936602769604 # 1
memory (df-graph) 142.5463671875 KiB (146.6995040110581) 142.5463671875 KiB (146.6995040110581) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.