-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fix bug issue 1554: using unique_ptr.reset to fix the SIGABRT error #1595
fix bug issue 1554: using unique_ptr.reset to fix the SIGABRT error #1595
Conversation
@@ -349,6 +349,7 @@ class PikaServer : public pstd::noncopyable { | |||
void InitStorageOptions(); | |||
|
|||
std::atomic<bool> exit_; | |||
std::timed_mutex exit_mutex_; | |||
|
|||
/* | |||
* Table used |
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.
The code patch adds a timed mutex named exit_mutex_
to the PikaServer
class, used in combination with the bool
atomic variable exit_
to synchronize access to the exit flag across threads.
Without seeing the rest of the codebase or knowing the specific use case, it's difficult to identify any bug risks or improvements. However, adding proper error handling in case the mutex cannot be acquired within the timeout period may be worth considering.
@@ -205,8 +215,5 @@ int main(int argc, char* argv[]) { | |||
// may references to dead PikaServer | |||
g_pika_rm->Stop(); | |||
|
|||
delete g_pika_server; | |||
::google::ShutdownGoogleLogging(); | |||
|
|||
return 0; | |||
} |
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.
Overall, the code patch seems to be a small addition of some header files and the implementation of resource cleanup using a defer macro. Here are some specific comments:
- The inclusion of new header files ("env.h" and "pstd_defer.h") seems fine.
- The addition of the defer block for cleanup is good practice to avoid memory leaks and other resource leakages during program termination.
- One minor suggestion is that instead of manually resetting all the unique_ptr objects in the defer block, you could call
reset()
on the unique_ptr that owns all the objects ("g_pika_conf") which would ensure they are automatically reset, and you can simplify your cleanup code. - Lastly, it's worth noting that the
close_std()
function called just before the defer block may have side effects, so you should make sure that it won't produce any undesired behavior when cleaning up resources.
There doesn't seem to be any obvious bug risk in this code patch, but a more comprehensive review of the entire program would be necessary to rule out any issues.
void PikaServer::Exit() { | ||
exit_mutex_.unlock(); | ||
exit_ = true; | ||
} | ||
|
||
std::string PikaServer::host() { return host_; } | ||
|
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.
The code patch adds a mutex to handle async exit for the server and changes how the server wakes up every 5 seconds instead of every 10 seconds.
Improvement suggestions:
- Instead of using
std::unique_ptr
to manage resources, consider usingstd::shared_ptr
if multiple objects need to manage the same resource. - Use a constant or variable to store the sleep interval (5 seconds) to avoid hardcoding it in the loop.
- Verify that
DoTimingTask()
and other parts of the code do not take too long, so that exiting the server can occur promptly upon receiving the signal.
Bug risks:
- If
DoTimingTask()
takes more than 5 seconds to execute, there may be a delay in exiting when the exit signal is received. - The order of unlocking the mutex and setting the exit flag should be consistent across all code paths to prevent potential race conditions.
@@ -205,8 +215,5 @@ int main(int argc, char* argv[]) { | |||
// may references to dead PikaServer | |||
g_pika_rm->Stop(); | |||
|
|||
delete g_pika_server; | |||
::google::ShutdownGoogleLogging(); | |||
|
|||
return 0; | |||
} |
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.
The code patch looks good in general, but here are some suggestions for improvement:
-
Consider using RAII (Resource Acquisition Is Initialization) instead of DEFER. Instead of wrapping the resource cleanup code inside the DEFER block, use appropriate destructors that can clean up resources automatically when the object goes out of scope. This will make the code clearer and less error-prone.
-
There is an unhandled exception possibility in this code which may cause a risk like resource leak or vulnerability with
g_pika_conf
. Consider using std::unique_ptr or another suitable smart pointer to manage g_pika_conf's lifecycle to ensure correct and safe allocation / deallocation of memory. -
Consider using C++11 style header inclusion (i.e.,
<cstdio>
instead of"stdio.h"
). This is considered good practice in modern C++ as it helps avoid potential naming conflicts and results in cleaner code. -
Consider refactoring the code into smaller functions or classes for better readability.
-
Avoid deleting the same resource twice. In the given code,
delete g_pika_server
and::google::ShutdownGoogleLogging()
are called twice, once inside DEFER and again outside it. It's better to delete these resources only once inside DEFER, especially if the DEFER block is wrapped around the main function's body. -
Consider catching exceptions and logging them instead of silently failing. It's usually better to catch exceptions and log them to help with debugging and system diagnosis.
-
Consider commenting on the purpose of the code, especially non-obvious pieces of code, poorly-named functions or variables.
Overall, the given code looks fairly safe and well-written code.
@@ -205,8 +216,5 @@ int main(int argc, char* argv[]) { | |||
// may references to dead PikaServer | |||
g_pika_rm->Stop(); | |||
|
|||
delete g_pika_server; | |||
::google::ShutdownGoogleLogging(); | |||
|
|||
return 0; | |||
} |
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.
The code patch looks mostly fine, here are some suggestions:
- Initialize pointers and variables to
nullptr
or appropriate default values in the declaration itself. - Use smart pointers (
std::unique_ptr
) instead of raw pointers where possible to avoid manual memory leaks. - Wrap main() with a try-catch block to handle exceptions, instead of allowing the program to crash.
- Add comments for any complex/nondescriptive sections of code if necessary.
Apart from that, here are some specific points related to your code patch:
- You don't need to include
pika_version.h
, as it's not used in this file. - It's good that you've included
pstd/include/pstd_defer.h
to delete objects at exit, but note that deferred objects may not be executed in case of an uncaught exception, so make sure to use try-catch blocks to handle all exceptions. - Before deleting
g_pika_server
, make sure it's not null to avoid undefined behavior. - Move the initialization of
g_pika_rm
,g_pika_cmd_table_manager
, andg_pika_conf
higher up in main, to avoid global static initialization issues such as the 'static initialization order fiasco'. - Consider using
std::make_unique()
instead ofnew
insidemain()
, and passing parameters to constructor directly.
void PikaServer::Exit() { | ||
exit_mutex_.unlock(); | ||
exit_ = true; | ||
} | ||
|
||
std::string PikaServer::host() { return host_; } | ||
|
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.
The patch looks good, but without any context or information about the system it's hard to provide an accurate review. Here are some general comments:
- The addition of a mutex lock in the constructor and unlocking it in Exit() suggests that the code is managing some shared resource across threads.
- The exit_ flag is set to true in Exit(), which will cause the while loop in Start() to terminate, leading to the shutdown of the PikaServer.
- The DoTimingTask() function is not shown, so its implementation cannot be reviewed.
…penAtomFoundation#1595) using unique_ptr.reset to fix the SIGABRT error && using time-wait lock to notify quickly
* feat: supported 'ptype' command * Simplified command `type` and `ptype` code * Remove the capacity judgment * fix slavaof serialize response bug (#1583) Co-authored-by: liuyuecai <[email protected]> * fix (#1587) (#1588) * fix unit test:type/set (#1577) * Fix memory leaks in HandleBGWorkerWriteDB and HandleMetaSyncResponse (#1590) * fix memory leaks in PikaReplBgWorker::HandleBGWorkerWriteDB(void* arg) and PikaReplClientConn::HandleMetaSyncResponse(void* arg). * add address/thread sanitizer to CMakeLists --------- Co-authored-by: cjh <[email protected]> * refactor: replace pstd/env with std::filesystem (#1470) * fix bug issue 1554: using unique_ptr.reset to fix the SIGABRT error (#1595) using unique_ptr.reset to fix the SIGABRT error && using time-wait lock to notify quickly * fix: incorrect manner of terminating the process (#1596) * fix issue#1597: add rocksdb dependency to pstd (#1599) * fix g++15 compile failure * add rocksdb dependency to pstd --------- Co-authored-by: J1senn <[email protected]> * fix_info_command * fix: gcc13 compile failed (#1601) * ci: add unit test to github action (#1604) * ci: add unit test in github workflow * chore:change `PLATFORM` field logic (#1615) * fix issue 1517: scan 命令不支持 type 参数 (#1582) * fix: the unit test of type/set (#1617) * test: optimize the return results of srandmember to avoid approximate results * fix: use last_seed for random engine * [fix issue1621] fix deadlock (#1620) * [fix] fix deadlock * [fix] fix * command `type` and `ptype` add unit test * fix member variable initialization * Update issue templates * feat: supported 'ptype' command * fix unit test:type/set (#1577) * fix issue#1597: add rocksdb dependency to pstd (#1599) * fix g++15 compile failure * add rocksdb dependency to pstd --------- Co-authored-by: J1senn <[email protected]> * fix: gcc13 compile failed (#1601) * fix: the unit test of type/set (#1617) * test: optimize the return results of srandmember to avoid approximate results * fix: use last_seed for random engine * Modify other modules to use the new `type` function * fix function repeat --------- Co-authored-by: Yuecai Liu <[email protected]> Co-authored-by: liuyuecai <[email protected]> Co-authored-by: Peter Chan <[email protected]> Co-authored-by: chenbt <[email protected]> Co-authored-by: Junhua Chen <[email protected]> Co-authored-by: cjh <[email protected]> Co-authored-by: kang jinci <[email protected]> Co-authored-by: Xin.Zh <[email protected]> Co-authored-by: machinly <[email protected]> Co-authored-by: A2ureStone <[email protected]> Co-authored-by: J1senn <[email protected]> Co-authored-by: chejinge <[email protected]> Co-authored-by: baerwang <[email protected]> Co-authored-by: ptbxzrt <[email protected]> Co-authored-by: 你不要过来啊 <[email protected]>
…penAtomFoundation#1595) using unique_ptr.reset to fix the SIGABRT error && using time-wait lock to notify quickly
* feat: supported 'ptype' command * Simplified command `type` and `ptype` code * Remove the capacity judgment * fix slavaof serialize response bug (OpenAtomFoundation#1583) Co-authored-by: liuyuecai <[email protected]> * fix (OpenAtomFoundation#1587) (OpenAtomFoundation#1588) * fix unit test:type/set (OpenAtomFoundation#1577) * Fix memory leaks in HandleBGWorkerWriteDB and HandleMetaSyncResponse (OpenAtomFoundation#1590) * fix memory leaks in PikaReplBgWorker::HandleBGWorkerWriteDB(void* arg) and PikaReplClientConn::HandleMetaSyncResponse(void* arg). * add address/thread sanitizer to CMakeLists --------- Co-authored-by: cjh <[email protected]> * refactor: replace pstd/env with std::filesystem (OpenAtomFoundation#1470) * fix bug issue 1554: using unique_ptr.reset to fix the SIGABRT error (OpenAtomFoundation#1595) using unique_ptr.reset to fix the SIGABRT error && using time-wait lock to notify quickly * fix: incorrect manner of terminating the process (OpenAtomFoundation#1596) * fix issue#1597: add rocksdb dependency to pstd (OpenAtomFoundation#1599) * fix g++15 compile failure * add rocksdb dependency to pstd --------- Co-authored-by: J1senn <[email protected]> * fix_info_command * fix: gcc13 compile failed (OpenAtomFoundation#1601) * ci: add unit test to github action (OpenAtomFoundation#1604) * ci: add unit test in github workflow * chore:change `PLATFORM` field logic (OpenAtomFoundation#1615) * fix issue 1517: scan 命令不支持 type 参数 (OpenAtomFoundation#1582) * fix: the unit test of type/set (OpenAtomFoundation#1617) * test: optimize the return results of srandmember to avoid approximate results * fix: use last_seed for random engine * [fix issue1621] fix deadlock (OpenAtomFoundation#1620) * [fix] fix deadlock * [fix] fix * command `type` and `ptype` add unit test * fix member variable initialization * Update issue templates * feat: supported 'ptype' command * fix unit test:type/set (OpenAtomFoundation#1577) * fix issue#1597: add rocksdb dependency to pstd (OpenAtomFoundation#1599) * fix g++15 compile failure * add rocksdb dependency to pstd --------- Co-authored-by: J1senn <[email protected]> * fix: gcc13 compile failed (OpenAtomFoundation#1601) * fix: the unit test of type/set (OpenAtomFoundation#1617) * test: optimize the return results of srandmember to avoid approximate results * fix: use last_seed for random engine * Modify other modules to use the new `type` function * fix function repeat --------- Co-authored-by: Yuecai Liu <[email protected]> Co-authored-by: liuyuecai <[email protected]> Co-authored-by: Peter Chan <[email protected]> Co-authored-by: chenbt <[email protected]> Co-authored-by: Junhua Chen <[email protected]> Co-authored-by: cjh <[email protected]> Co-authored-by: kang jinci <[email protected]> Co-authored-by: Xin.Zh <[email protected]> Co-authored-by: machinly <[email protected]> Co-authored-by: A2ureStone <[email protected]> Co-authored-by: J1senn <[email protected]> Co-authored-by: chejinge <[email protected]> Co-authored-by: baerwang <[email protected]> Co-authored-by: ptbxzrt <[email protected]> Co-authored-by: 你不要过来啊 <[email protected]>
…penAtomFoundation#1595) using unique_ptr.reset to fix the SIGABRT error && using time-wait lock to notify quickly
* feat: supported 'ptype' command * Simplified command `type` and `ptype` code * Remove the capacity judgment * fix slavaof serialize response bug (OpenAtomFoundation#1583) Co-authored-by: liuyuecai <[email protected]> * fix (OpenAtomFoundation#1587) (OpenAtomFoundation#1588) * fix unit test:type/set (OpenAtomFoundation#1577) * Fix memory leaks in HandleBGWorkerWriteDB and HandleMetaSyncResponse (OpenAtomFoundation#1590) * fix memory leaks in PikaReplBgWorker::HandleBGWorkerWriteDB(void* arg) and PikaReplClientConn::HandleMetaSyncResponse(void* arg). * add address/thread sanitizer to CMakeLists --------- Co-authored-by: cjh <[email protected]> * refactor: replace pstd/env with std::filesystem (OpenAtomFoundation#1470) * fix bug issue 1554: using unique_ptr.reset to fix the SIGABRT error (OpenAtomFoundation#1595) using unique_ptr.reset to fix the SIGABRT error && using time-wait lock to notify quickly * fix: incorrect manner of terminating the process (OpenAtomFoundation#1596) * fix issue#1597: add rocksdb dependency to pstd (OpenAtomFoundation#1599) * fix g++15 compile failure * add rocksdb dependency to pstd --------- Co-authored-by: J1senn <[email protected]> * fix_info_command * fix: gcc13 compile failed (OpenAtomFoundation#1601) * ci: add unit test to github action (OpenAtomFoundation#1604) * ci: add unit test in github workflow * chore:change `PLATFORM` field logic (OpenAtomFoundation#1615) * fix issue 1517: scan 命令不支持 type 参数 (OpenAtomFoundation#1582) * fix: the unit test of type/set (OpenAtomFoundation#1617) * test: optimize the return results of srandmember to avoid approximate results * fix: use last_seed for random engine * [fix issue1621] fix deadlock (OpenAtomFoundation#1620) * [fix] fix deadlock * [fix] fix * command `type` and `ptype` add unit test * fix member variable initialization * Update issue templates * feat: supported 'ptype' command * fix unit test:type/set (OpenAtomFoundation#1577) * fix issue#1597: add rocksdb dependency to pstd (OpenAtomFoundation#1599) * fix g++15 compile failure * add rocksdb dependency to pstd --------- Co-authored-by: J1senn <[email protected]> * fix: gcc13 compile failed (OpenAtomFoundation#1601) * fix: the unit test of type/set (OpenAtomFoundation#1617) * test: optimize the return results of srandmember to avoid approximate results * fix: use last_seed for random engine * Modify other modules to use the new `type` function * fix function repeat --------- Co-authored-by: Yuecai Liu <[email protected]> Co-authored-by: liuyuecai <[email protected]> Co-authored-by: Peter Chan <[email protected]> Co-authored-by: chenbt <[email protected]> Co-authored-by: Junhua Chen <[email protected]> Co-authored-by: cjh <[email protected]> Co-authored-by: kang jinci <[email protected]> Co-authored-by: Xin.Zh <[email protected]> Co-authored-by: machinly <[email protected]> Co-authored-by: A2ureStone <[email protected]> Co-authored-by: J1senn <[email protected]> Co-authored-by: chejinge <[email protected]> Co-authored-by: baerwang <[email protected]> Co-authored-by: ptbxzrt <[email protected]> Co-authored-by: 你不要过来啊 <[email protected]>
fix #1554