Kitura / Kitura-CouchDB

CouchDB adapter for Kitura
Apache License 2.0
51 stars 29 forks source link

Creating a user with usersDatabase().createUser() will never return and use memory until crash #51

Closed nikwest closed 7 years ago

nikwest commented 7 years ago

When calling the method usersDatabase().createUser() on couchDBClient it will never return. Is seems to get stuck in an endless loop in the network stack. The user will be created in couchDB, but the call never returns.

Here is my test code:

index f8bcf6b..c5f385d 100644
--- i/Sources/CouchDBSample/main.swift
+++ w/Sources/CouchDBSample/main.swift
@@ -179,8 +179,32 @@ func deleteDocument(revisionNumber: String) {
     })
 }

+func createUser() {
+  let newUser : [String:Any] = [
+    "name" : "testuser",
+    "type": "user",
+    "password": "apple",
+    "roles": []
+  ]
+
+  couchDBClient.usersDatabase().createUser(document: JSON(newUser)) {
+    id, doc, error in
+    guard let id = id,
+      let doc = doc,
+      error == nil else {
+        Log.error("User could not be created due to error: (error.description())")
+        return
+    }
+    Log.info("created user \(id) with doc: \(doc.debugDescription)")
+  }
+}
+
+
+

 // Start tests...
-createDocument()
+//createDocument()
+
+createUser()

 Log.info("Sample program completed its execution.")

I run it against a fresh, local couchDB 2.0 instance.

The stack trace is:

#1  0x00000001001f0eab in HTTPIncomingMessage.parsingCompleted() -> () at /Users/dominik/Developer/Colorimetrix/BunBun-Server/Packages/Kitura-net-1.4.3/Sources/KituraNet/HTTP/HTTPIncomingMessage.swift:253
#2  0x00000001001eec8d in HTTPIncomingMessage.parse(NSData, from : Int, completeBuffer : Bool) -> HTTPParserStatus at /Users/dominik/Developer/Colorimetrix/BunBun-Server/Packages/Kitura-net-1.4.3/Sources/KituraNet/HTTP/HTTPIncomingMessage.swift:169
#3  0x00000001001c38e7 in ClientResponse.parse() -> HTTPParserStatus at /Users/dominik/Developer/Colorimetrix/BunBun-Server/Packages/Kitura-net-1.4.3/Sources/KituraNet/ClientResponse.swift:57
#4  0x00000001001bd68d in ClientRequest.end(close : Bool) -> () at /Users/dominik/Developer/Colorimetrix/BunBun-Server/Packages/Kitura-net-1.4.3/Sources/KituraNet/ClientRequest.swift:344
#5  0x00000001001bceee in ClientRequest.end(String, close : Bool) -> () at /Users/dominik/Developer/Colorimetrix/BunBun-Server/Packages/Kitura-net-1.4.3/Sources/KituraNet/ClientRequest.swift:291
#6  0x00000001003560e1 in UsersDatabase.createUser(document : JSON, callback : (String?, JSON?, NSError?) -> ()) -> () at /Users/dominik/Developer/Colorimetrix/BunBun-Server/Packages/Kitura-CouchDB-1.4.0/Sources/CouchDB/UsersDatabase.swift:55
#7  0x0000000100007c3d in createUser() -> () at /Users/dominik/Developer/Colorimetrix/BunBun-Server/Packages/Kitura-CouchDB-1.4.0/Sources/CouchDBSample/main.swift:199
#8  0x00000001000035ae in main at /Users/dominik/Developer/Colorimetrix/BunBun-Server/Packages/Kitura-CouchDB-1.4.0/Sources/CouchDBSample/main.swift:208

I tested with the latest released Kitura stack

CloudFoundryEnv-1.9.2
HeliumLogger-1.4.0
Kitura-1.4.2
Kitura-CouchDB-1.4.0
Kitura-TemplateEngine-1.4.0
Kitura-net-1.4.3
LoggerAPI-1.4.0
SSLService-0.12.10
Socket-0.12.16
SwiftyJSON-15.0.4

It's probably a Kitura-net problem, but I'm just starting using the stack and not familiar with the low level stuff.

Spriter commented 7 years ago

Any news about this problem ? I got a similar problem with the update method.

pbohrer commented 7 years ago

Hmm. I tried to reproduce and while I don't have couch 2.0 installed anywhere, this works fine against Couch 1.5 and 1.6. On what system are you running this ?

Spriter commented 7 years ago

I tested my application with CouchDB 1.6.1 now and the problem is away. It seems to be only a problem with couch db 2.0

pbohrer commented 7 years ago

CouchDB 2.0 on macOS worked fine. I just reproduced this problem on Ubuntu 16.04 with CouchDB 2.0. When connected to CouchDB 2.0 on Linux, we seem to get stuck in the repeat statement at line 343: https://github.com/IBM-Swift/Kitura-net/blob/master/Sources/KituraNet/ClientRequest.swift

Spriter commented 7 years ago

For me this problem also exists on MacOS 10.11.6.

jackd942 commented 7 years ago

Same issue on MacOS 10.12.2 with update() on clean install of CouchDB 2.0.0 as well as Cloudant on Bluemix. CouchDB 1.6.1 seems to work fine. Here is some test code and output in Xcode:

func updateDoc() {
  Log.info("Updating document")

  let id = "4eb3659b5c75c61caa985118a6000897"
  let rev = "3-987be9bdf943f7368b87fafb725f09a2"

  let json = JSON([
    "_id": id,
    "_rev": rev,
    "name": "updated test 1",
    "value": 1
    ])

  database.update(id, rev: rev, document: json) { rev, doc, err in
    if err != nil {
      Log.info("Update failed")
      return
    }
    Log.info("Completed Update")
    Log.info("New Revision: \(rev!)")
  }
}

updateDoc()

Output for CouchDB 1.6.1

[2017-01-21T15:34:48.998-06:00] [INFO] [main.swift:26 updateDoc()] Updating document
[2017-01-21T15:34:49.016-06:00] [INFO] [main.swift:45 updateDoc()] Completed Update
[2017-01-21T15:34:49.016-06:00] [INFO] [main.swift:46 updateDoc()] New Revision: 4-a974df830c2742e1d72a3b76bf21ce62
Program ended with exit code: 0

Output for CouchDB 2.0.0 - Starts and then hangs until it is killed (Same output for Bluemix Cloudant)

[2017-01-21T15:40:23.180-06:00] [INFO] [main.swift:26 updateDoc()] Updating document
Message from debugger: failed to send the k packet
Program ended with exit code: 6
shmuelk commented 7 years ago

I have recreated this problem on macOS using Kitura-CouchDB's own unit tests.

The problem is in KituraNet. I'm fixing this under issue IBM-Swift/Kitura#980

na-gupta commented 7 years ago

@nikwest @Spriter @jackd942 The fix has been tagged in Kitura-net 1.5.2 Could you please validate that a fresh pull of Kitura and Kitura-net 1.5 fixes this issue for you.

jackd942 commented 7 years ago

@na-gupta @shmuelk All seems well. Thanks for the work!

Spriter commented 7 years ago

@na-gupta All seems to work fine. Thanks for the fix!