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

files, sockets: ignore cleanup exceptions on =destroy #78

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion src/sys/private/files_posix.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ template closeImpl() {.dirty.} =
close f.handle

template destroyFileImpl() {.dirty.} =
cleanupFile f
try:
cleanupFile f
except CatchableError:
discard "Nothing can be done here"

`=destroy` f.handle

template newFileImpl() {.dirty.} =
Expand Down
6 changes: 5 additions & 1 deletion src/sys/private/files_windows.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ template closeImpl() {.dirty.} =
close f.handle

template destroyFileImpl() {.dirty.} =
cleanupFile f
try:
cleanupFile f
except CatchableError:
discard "Nothing can be done here"

`=destroy` f.handle

template newFileImpl() {.dirty.} =
Expand Down
17 changes: 13 additions & 4 deletions src/sys/sockets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,30 @@
## If `s` is invalid, `ClosedHandleDefect` will be raised.
close s.handle

proc close(s: var AsyncSocketObj) {.inline.}
template closeAsync(s: untyped) =
## Forward declaration of `close(AsyncSocketObj)` to avoid issues due to
## `=destroy` being implicitly defined at first usage of type
unregister s.handle.fd
close SocketObj(s)

proc `=destroy`(s: var AsyncSocketObj) =
## Destroy the asynchronous socket `s`.
##
## Note that non-fatal errors are ignored, use `close` instead if those should
## be catched.
if s.handle.fd != InvalidFD:
close(s)
try:
closeAsync(s)
except CatchableError:
discard "Nothing can be done here"

proc close(s: var AsyncSocketObj) {.inline.} =
## Closes and invalidates the socket `s`.
##
## If `s` is invalid, `ClosedHandleDefect` will be raised.
##
## The FD associated with `s` will be deregistered from `ioqueue`.
unregister s.handle.fd
close SocketObj(s)
closeAsync(s)

proc close*(s: Socket) {.inline.} =
## Closes and invalidates the socket `s`.
Expand All @@ -86,7 +95,7 @@
close s[]

# These are used to ensure correct destructor binding
{.warning: "compiler bug workaround; see: https://github.com/nim-lang/Nim/issues/19138".}

Check warning on line 98 in src/sys/sockets.nim

View workflow job for this annotation

GitHub Actions / linux on amd64 (nim devel)

compiler bug workaround; see: https://github.com/nim-lang/Nim/issues/19138 [User]
proc newSocketAs(T: typedesc[not ref], fd: SocketFD): ref T {.inline.} =
## Create a ref socket from `fd`, converted to `T`.
new result
Expand Down Expand Up @@ -498,7 +507,7 @@
var
attempted = false
lastError: ref OSError
{.warning: "Workaround for nim-works/cps#185".}

Check warning on line 510 in src/sys/sockets.nim

View workflow job for this annotation

GitHub Actions / linux on amd64 (nim devel)

Workaround for nim-works/cps#185 [User]
let next: iterator (): IPEndpoint = closureItems(endpoints)
while true:
attempted = true
Expand Down
Loading