Skip to content

fix file descriptor leak in example http server code#4

Open
adamyi wants to merge 2 commits intoajyoon:masterfrom
adamyi:master
Open

fix file descriptor leak in example http server code#4
adamyi wants to merge 2 commits intoajyoon:masterfrom
adamyi:master

Conversation

@adamyi
Copy link
Copy Markdown

@adamyi adamyi commented Jul 6, 2020

Originally the code only closed the fd for the connection but not for the file.

I'm running this code in prod and it ran out of fds fairly quickly. This fix makes it more stable :)

@adamyi
Copy link
Copy Markdown
Author

adamyi commented Mar 23, 2022

Hey @ajyoon, just a friendly ping on this :)

Copy link
Copy Markdown
Owner

@ajyoon ajyoon left a comment

Choose a reason for hiding this comment

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

apologies for my delay in patching in this very important production server fix. It seems correct enough to me, but I have a commentary question

Comment thread examples/http/server.bf Outdated
Comment on lines +561 to +567
Move to cell 51
>>>

Close the opened file

The current cell is 49
The source file descriptor is in cell 53
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The comment above says it moves to cell 51, but this comment here says it's 49?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That is confusing.

@adamyi
Copy link
Copy Markdown
Author

adamyi commented Aug 29, 2025

@ajyoon @kailando
I am very sorry about the delay in fixing the comment for this very important production server fix, and the confusion this has caused.

I verified that the comment was wrong, but the code was correct.

According to sendfile call:

  Currently in cell 48
  ++++++++++++++++++++++++++++++++++++++++ > sendfile() == code 40
  ++++ > Arg count: 4

  Arg 1: int out_fd
  (0) > Arg type: Normal
  +   > Arg len:  1
  Arg contents are currently stored in cell 4; go fetch it
  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  [ -
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    +
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ]
  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  >

  Arg 2: int in_fd
  (0) > Arg type: Normal
  +   > Arg len:  1
  Arg contents are currently stored in cell 65; go fetch it
  >>>>>>>>>>
  [ -
    <<<<<<<<<<
    +
    >>>>>>>>>>
  ]
  <<<<<<<<<<
  >

Memory layout is that:

Cell 48: sendfile code
Cell 49: arg count 4
Cell 50: arg type
cell 51: arg len 1
Cell 52: out_fd
Cell 53: arg_type
cell 54: arg len 1
Cell 55: in_fd

Our previous close call ended at cell 48. Indeed, moving it right by 3 would move to 51, and we're emptying out 51-54 so that 55 can be the first argument for close syscall. After close syscall, we shift left by 3, and would indeed return back to 48.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants