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

KERNEL: Correctly using ST threads, an HTTP request shorter than 10 milliseconds may cause a segmentation fault. #110

Closed
winlinvip opened this issue May 1, 2015 · 1 comment
Assignees
Labels
TransByAI Translated by AI/GPT.
Milestone

Comments

@winlinvip
Copy link
Member

winlinvip commented May 1, 2015

https://github.com/winlinvip/simple-rtmp-server/issues/110

TRANS_BY_GPT3

@winlinvip winlinvip added this to the srs 1.0 release milestone May 1, 2015
@winlinvip
Copy link
Member Author

winlinvip commented Mar 1, 2017

If the thread is terminated immediately after creation, interrupt can still take effect, for example:

void* pfn(void* arg)
{
    trace("Enter thread, sleep.");
    st_sleep(10);
    trace("Quit thread.");
    return NULL;
}

    trace("Create thread.");
    st_thread_t trd = st_thread_create(pfn, NULL, 1, 0);
    trace("Interrupt thread.");
    st_thread_interrupt(trd);
    trace("Join thread.");
    st_thread_join(trd, NULL);
    trace("Thread done.");

Output:

[T][76189][2017-03-01 12:17:08.33] Create thread.
[T][76189][2017-03-01 12:17:08.33] Interrupt thread.
[T][76189][2017-03-01 12:17:08.33] Join thread.
[T][76189][2017-03-01 12:17:08.33] Enter thread, sleep.
[T][76189][2017-03-01 12:17:08.33] Quit thread.
[T][76189][2017-03-01 12:17:08.33] Thread done.

This indicates that the thread function pfn does not need to consider the situation of being terminated before it is started.

  1. After the thread is started, it enters a blocking state, and the function can also be interrupted by an interrupt.
void* pfn(void* arg)
{
    trace("Enter thread, sleep.");
    st_sleep(10);
    trace("Quit thread.");
    return NULL;
}

    trace("Create thread.");
    st_thread_t trd = st_thread_create(pfn, NULL, 1, 0);
    st_sleep(1);
    trace("Interrupt thread.");
    st_thread_interrupt(trd);
    trace("Join thread.");
    st_thread_join(trd, NULL);
    trace("Thread done.");

After 1 second, the interrupt function is called. At this time, the thread has already started and entered the waiting state. The interrupt is effective and the output is as follows:

[T][76286][2017-03-01 12:19:24.95] Create thread.
[T][76286][2017-03-01 12:19:24.95] Enter thread, sleep.
[T][76286][2017-03-01 12:19:25.95] Interrupt thread.
[T][76286][2017-03-01 12:19:25.95] Join thread.
[T][76286][2017-03-01 12:19:25.95] Quit thread.
[T][76286][2017-03-01 12:19:25.95] Thread done.

This indicates that if it is only an IO operation (without a loop), there is no need to use a state variable. It can be directly handled.

  1. If there is a loop inside the thread, such as a thread that receives messages, then a variable is needed.
void* pfn(void* arg)
{
    bool* loop = (bool*)arg;
    trace("Enter thread.");
    while (*loop) {
        trace("Thread sleep");
        st_sleep(10);
        trace("Thread aweak");
    }
    trace("Quit thread.");
    return NULL;
}

    trace("Create thread.");
    bool loop = true;
    st_thread_t trd = st_thread_create(pfn, &loop, 1, 0);
    st_sleep(1);
    trace("Interrupt thread.");
    loop = false;
    st_thread_interrupt(trd);
    trace("Join thread.");
    st_thread_join(trd, NULL);
    trace("Thread done.");

Under the influence of the variables "loop" and "interrupt", the thread exits normally, and the result is as follows:

[T][76401][2017-03-01 12:22:27.07] Create thread.
[T][76401][2017-03-01 12:22:27.07] Enter thread.
[T][76401][2017-03-01 12:22:27.07] Thread sleep
[T][76401][2017-03-01 12:22:28.07] Interrupt thread.
[T][76401][2017-03-01 12:22:28.07] Join thread.
[T][76401][2017-03-01 12:22:28.07] Thread aweak
[T][76401][2017-03-01 12:22:28.07] Quit thread.
[T][76401][2017-03-01 12:22:28.07] Thread done.

Based on the above considerations, the lifecycle of the ST thread can be simplified: link

TRANS_BY_GPT3

@winlinvip winlinvip changed the title 0.9.134,http请求短于10毫秒可能导致段错误 KERNEL: 正确使用ST线程,http请求短于10毫秒可能导致段错误 Mar 1, 2017
winlinvip added a commit that referenced this issue Mar 1, 2017
@winlinvip winlinvip self-assigned this Aug 26, 2021
@winlinvip winlinvip changed the title KERNEL: 正确使用ST线程,http请求短于10毫秒可能导致段错误 KERNEL: Correctly using ST threads, an HTTP request shorter than 10 milliseconds may cause a segmentation fault. Jul 29, 2023
@winlinvip winlinvip added the TransByAI Translated by AI/GPT. label Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TransByAI Translated by AI/GPT.
Projects
None yet
Development

No branches or pull requests

1 participant