BlueSocket icon indicating copy to clipboard operation
BlueSocket copied to clipboard

multi-process problem

Open shanksyang opened this issue 8 years ago • 13 comments

When I use this library to integrate into Linux multi-process swift code, it occurs a fatal error while call the acceptClientConnection method, error message is : -9994(0x-270A), Invalid argument

the code is below:

do {
    var socket: Socket? = try Socket.create()
    
    guard let listenSocket = socket else {
        print("unable to create socket...")
        exit(-1)
    }
    
    var isRunning = true
    try listenSocket.listen(on: 6666)
    
    repeat {
        print(listenSocket.signature?.description)
        let connectSocket = try listenSocket.acceptClientConnection()
        print("Accepted connection from: \(connectSocket.remoteHostname) on port \(connectSocket.remotePort)")
        //print("Socket Signature: \(connectSocket.signature?.description)")
        
        var childpid = fork()
        
        if childpid == -1 {
            print("fork failed")
            exit(-1)
        } else if childpid > 0 {
            print("parent process")
            connectSocket.close()
            isRunning = true
        } else {
            print("child process")
            listenSocket.close()
            handleReqeust(connectSocket)
            isRunning = false
        }

        
    } while isRunning
    
    
} catch let error {
    guard let socketError = error as? Socket.Error else {
        print("unknown error...")
        exit(-1)
    }
    print("Error reported:\n \(socketError.description)")

}

shanksyang avatar May 10 '17 10:05 shanksyang

When I attempt to compile your test program, I get the following error:

error: 'fork()' is unavailable: Please use threads or posix_spawn*()

It appears that fork() is no longer supported by the Swift runtime.

billabt avatar May 10 '17 15:05 billabt

@billabt my compilation is ok, based on ubuntu 14.04 and swift 3, which platform and swift version do you use?

shanksyang avatar May 10 '17 15:05 shanksyang

I used Swift 3.1 and tried it on both macOS and ubuntu 16.04. fork support was purposely dropped in Swift 3.1 for issues like the one you're running into.

billabt avatar May 10 '17 15:05 billabt

@shanksyang Just to be clear, are you using Swift 3.1? swift --version

kweinmeister avatar May 10 '17 15:05 kweinmeister

@billabt @kweinmeister ok, let me try alternative method using Swift 3.1

shanksyang avatar May 10 '17 15:05 shanksyang

Let me know how you make out. Thanks.

billabt avatar May 10 '17 15:05 billabt

@billabt I used swift 3.1.1 on ubuntu 14.04, fork is ok, but the error is the same as before..

shanksyang avatar May 11 '17 05:05 shanksyang

let me try on ubuntu 16.04, but I need find this system to build .., back soon

shanksyang avatar May 11 '17 05:05 shanksyang

I'll look into it a bit deeper here as well... I'll post anything I find. Thanks.

billabt avatar May 11 '17 13:05 billabt

@shanksyang: I was able to reproduce the error on 16.04 with the released version of Swift 3.1.1. I had been still using the development version of Swift 3.1.1 prior which gave the error I alluded to earlier. Strangely, the error still appears when I build the app on macOS so I'm wondering if the fact I'm no longer getting the error on Linux is a bug since they've obviously excluded use of fork() on macOS. In any event, it appears that one of the parameters being passed to the accept() API is not valid. However, I've confirmed that the parameters are all correct and the file descriptor is still open. As a matter of fact, they're identical to the parameters passed in the first call to accept() which worked. I'm going to continue to look at it a bit deeper and (if I have to) build a debug version of Glibc to try to debug it further if necessary. But first, I'm going see if I can reproduce the problem using a simple C-based program and see what that turns up. I'll keep you posted.

billabt avatar May 11 '17 17:05 billabt

@billabt I have encountered the same thing you posted,I was busy last week,2017@Swift China has been held in Shenzhen, I will continue to debug this issue this weekend. do you use strace to debug?

shanksyang avatar May 18 '17 16:05 shanksyang

@shanksyang: I'm gonna write a C-based version of the program to compare the flow with the Swift version. The flow should almost be identical. I've been debugging it using a lldb and a debug version of Glibc. It's failing deep into the accept API but I haven't been able to determine the reason for it yet although it's beginning to look like it might be a Swift runtime bug, possibly a memory overwrite. I'm getting close. I'll keep you posted.

billabt avatar May 19 '17 14:05 billabt

Is this still an issue? Trying to clean up the issue list. Thanks.

billabt avatar Nov 06 '18 13:11 billabt