Вопрос

I have an stunnel build for iOS and it works perfectly, but when I try to restart it app crashes.

I'm starting it with pthread_create. Here is the code to restart.

int ret;
ret = pthread_cancel(old_threadID);
if (ret != 0)
    NSLog(@"%i", ret);

void *res;
ret = pthread_join(old_threadID, &res);
if (ret != 0)
    NSLog(@"%i", ret);

if (res == PTHREAD_CANCELED)
    NSLog(@"main(): thread was canceled\n"); //This returns OK
else
    NSLog(@"main(): thread wasn't canceled (shouldn't happen!)\n");

//Begin new thread
pthread_t threadID;
//launch stunnel in a thread
int errorStunnel = pthread_create(&threadID, NULL, &stunnel_routine, (void *)fname);
NSLog(@"Error code!! : %i", errorStunnel); //error is 0.

After this App crashes. It crashes because can not start properly. as I see in stunnel.log because of this: "Error binding :443 to 127.0.0.1:12345 bind: Address already in use (48)" Config file didn't change. So why ports are still binded with previous thread?

Это было полезно?

Решение

Did you close the socket when you canceled the thread? The socket file descriptor is shared by the entire process. If you just blow away the thread the socket is still open and bound to that port because the network layer has no idea nor concern with how many threads you are running. It only knows that the process still has the fd open.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top