-
Notifications
You must be signed in to change notification settings - Fork 14
CP-28365: improve backtraces by using finally #32
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f00572e
CP-28365: do not loose the backtrace in Mutex.execute
edwintorok 2e5f211
CP-28365: do not loose backtraces in Mutex.execute
edwintorok 90178da
CP-28365: improve backtraces in with_file
edwintorok 5401c04
CP-28365: improve backtraces in daemonize
edwintorok adba34e
CP-28365: improve backtraces in Unixext.open_connection*
edwintorok b78eb0e
CP-28365: improve stacktraces in Semaphore.execute_with_weight
edwintorok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,13 @@ let pidfile_read filename = | |
| with _ -> None) | ||
| (fun () -> Unix.close fd) | ||
|
|
||
| (** open a file, and make sure the close is always done *) | ||
| let with_file file mode perms f = | ||
| let fd = Unix.openfile file mode perms in | ||
| Xapi_stdext_pervasives.Pervasiveext.finally | ||
| (fun () -> f fd) | ||
| (fun () -> Unix.close fd) | ||
|
|
||
| (** daemonize a process *) | ||
| (* !! Must call this before spawning any threads !! *) | ||
| let daemonize () = | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should really deprecate this function... nowadays it's systemd duty to damonize
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened a ticket, CP-28369 |
||
|
|
@@ -73,14 +80,11 @@ let daemonize () = | |
|
|
||
| begin match Unix.fork () with | ||
| | 0 -> | ||
| let nullfd = Unix.openfile "/dev/null" [ Unix.O_WRONLY ] 0 in | ||
| begin try | ||
| Unix.close Unix.stdin; | ||
| Unix.dup2 nullfd Unix.stdout; | ||
| Unix.dup2 nullfd Unix.stderr; | ||
| with exn -> Unix.close nullfd; raise exn | ||
| end; | ||
| Unix.close nullfd | ||
| with_file "/dev/null" [ Unix.O_WRONLY ] 0 | ||
| (fun nullfd -> | ||
| Unix.close Unix.stdin; | ||
| Unix.dup2 nullfd Unix.stdout; | ||
| Unix.dup2 nullfd Unix.stderr) | ||
| | _ -> exit 0 | ||
| end | ||
| | _ -> exit 0 | ||
|
|
@@ -115,15 +119,6 @@ let with_input_channel file f = | |
| (fun () -> f input) | ||
| (fun () -> close_in input) | ||
|
|
||
| (** open a file, and make sure the close is always done *) | ||
| let with_file file mode perms f = | ||
| let fd = Unix.openfile file mode perms in | ||
| let r = | ||
| try f fd | ||
| with exn -> Unix.close fd; raise exn | ||
| in | ||
| Unix.close fd; | ||
| r | ||
|
|
||
| let file_lines_fold f start file_path = with_input_channel file_path (lines_fold f start) | ||
|
|
||
|
|
@@ -148,12 +143,9 @@ let fd_blocks_fold block_size f start fd = | |
|
|
||
| let with_directory dir f = | ||
| let dh = Unix.opendir dir in | ||
| let r = | ||
| try f dh | ||
| with exn -> Unix.closedir dh; raise exn | ||
| in | ||
| Unix.closedir dh; | ||
| r | ||
| Xapi_stdext_pervasives.Pervasiveext.finally | ||
| (fun () -> f dh) | ||
| (fun () -> Unix.closedir dh) | ||
|
|
||
| let buffer_of_fd fd = | ||
| fd_blocks_fold 1024 (fun b s -> Buffer.add_bytes b s; b) (Buffer.create 1024) fd | ||
|
|
@@ -262,6 +254,7 @@ let open_connection_fd host port = | |
| connect s ai.ai_addr; | ||
| s | ||
| with e -> | ||
| Backtrace.is_important e; | ||
| close s; | ||
| raise e | ||
|
|
||
|
|
@@ -271,7 +264,10 @@ let open_connection_unix_fd filename = | |
| let addr = Unix.ADDR_UNIX(filename) in | ||
| Unix.connect s addr; | ||
| s | ||
| with e -> Unix.close s; raise e | ||
| with e -> | ||
| Backtrace.is_important e; | ||
| Unix.close s; | ||
| raise e | ||
|
|
||
| module CBuf = struct | ||
| (** A circular buffer constructed from a string *) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could even expose this in the interface
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, it's there!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It already is, I just moved it.