Browse code

c-rest-engine: Instance state for API safety

Change-Id: Ia9d9644eb7bb38110a48e97daa59cfbb726213e2
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/4785
Reviewed-by: Kumar Kaushik <kaushikk@vmware.com>
Tested-by: Kumar Kaushik <kaushikk@vmware.com>

Kumar Kaushik authored on 2018/02/14 17:16:39
Showing 2 changed files
... ...
@@ -1,7 +1,7 @@
1 1
 Name:          c-rest-engine
2 2
 Summary:       minimal http(s) server library
3 3
 Version:       1.1
4
-Release:       9%{?dist}
4
+Release:       10%{?dist}
5 5
 Group:         Applications/System
6 6
 Vendor:        VMware, Inc.
7 7
 License:       Apache 2.0
... ...
@@ -21,6 +21,7 @@ Patch5:        minimal_request_logging.patch
21 21
 Patch6:        connection_timeout.patch
22 22
 Patch7:        reqLine_parsing_check.patch
23 23
 Patch8:        bad_mem_write.patch
24
+Patch9:        instance_state.patch
24 25
 %define sha1   c-rest-engine=a25927fd98ec92df5e210cc4941fa626604636f6
25 26
 
26 27
 %description
... ...
@@ -48,6 +49,7 @@ development libs and header files for c-rest-engine
48 48
 %patch6 -p1
49 49
 %patch7 -p1
50 50
 %patch8 -p1
51
+%patch9 -p1
51 52
 
52 53
 %build
53 54
 cd build
... ...
@@ -80,6 +82,8 @@ find %{buildroot} -name '*.la' -delete
80 80
 # %doc ChangeLog README COPYING
81 81
 
82 82
 %changelog
83
+*  Wed Feb 14 2018 Kumar Kaushik <kaushikk@vmware.com> 1.1-10
84
+-  Maintaing instance state for API calls safety.
83 85
 *  Tue Feb 06 2018 Kumar Kaushik <kaushikk@vmware.com> 1.1-9
84 86
 -  Fixing bad memory write crash.
85 87
 *  Mon Jan 29 2018 Kumar Kaushik <kaushikk@vmware.com> 1.1-8
86 88
new file mode 100644
... ...
@@ -0,0 +1,1294 @@
0
+From e4262558a10e4470dab84d50b5f7d8d4121e8d1d Mon Sep 17 00:00:00 2001
1
+From: Kumar Kaushik <kaushikk@vmware.com>
2
+Date: Mon, 12 Feb 2018 20:03:44 -0800
3
+Subject: [PATCH] Maintaining internal library instanse state for API safety
4
+
5
+Change-Id: Icf1d75eaba6d62c3486653ffa1ae7528ced620e3
6
+---
7
+ common/sockinterface.c              |  88 +++++++++++++++--
8
+ include/public/vmrest.h             |   1 +
9
+ include/vmrestcommon.h              |  16 ++-
10
+ include/vmsock.h                    |  39 ++++++--
11
+ server/restengine/defines.h         |  16 ++-
12
+ server/restengine/httpAllocStruct.c |   2 +
13
+ server/restengine/httpMain.c        |  39 +++++---
14
+ server/restengine/libmain.c         | 142 ++++++++++++++++++++-------
15
+ transport/api/api.c                 |  31 +++++-
16
+ transport/posix/libmain.c           |   5 +-
17
+ transport/posix/prototypes.h        |  11 ++-
18
+ transport/posix/socket.c            | 187 +++++++++++++++++++-----------------
19
+ 12 files changed, 414 insertions(+), 163 deletions(-)
20
+
21
+diff --git a/common/sockinterface.c b/common/sockinterface.c
22
+index 9e7d404..f737f78 100644
23
+--- a/common/sockinterface.c
24
+@@ -71,6 +71,40 @@ VmRESTInitProtocolServer(
25
+     )
26
+ {
27
+     DWORD                            dwError = REST_ENGINE_SUCCESS;
28
++
29
++    if (!pRESTHandle)
30
++    {
31
++        VMREST_LOG_ERROR(pRESTHandle,"%s","Invalid REST config");
32
++        dwError = REST_ERROR_INVALID_HANDLER;
33
++    }
34
++    BAIL_ON_VMREST_ERROR(dwError);
35
++
36
++    /**** Init Logging ****/
37
++    dwError = VmRESTLogInitialize(
38
++                  pRESTHandle
39
++                  );
40
++    BAIL_ON_VMREST_ERROR(dwError);
41
++
42
++    /**** Init Transport ****/
43
++    dwError = VmwSockInitialize(pRESTHandle);
44
++    BAIL_ON_VMREST_ERROR(dwError);
45
++
46
++cleanup:
47
++
48
++    return dwError;
49
++
50
++error:
51
++
52
++    goto cleanup;
53
++
54
++}
55
++
56
++DWORD
57
++VmRESTStartProtocolServer(
58
++    PVMREST_HANDLE                   pRESTHandle
59
++    )
60
++{
61
++    DWORD                            dwError = REST_ENGINE_SUCCESS;
62
+     PVMREST_SOCK_CONTEXT             pSockContext = NULL;
63
+     DWORD                            dwFlags = VM_SOCK_CREATE_FLAGS_REUSE_ADDR |
64
+                                                VM_SOCK_CREATE_FLAGS_NON_BLOCK;
65
+@@ -92,7 +126,7 @@ VmRESTInitProtocolServer(
66
+ 
67
+     /**** Handle IPv4 case ****/
68
+ 
69
+-    dwError = VmwSockOpenServer(
70
++    dwError = VmwSockStartServer(
71
+                          pRESTHandle,
72
+                         dwFlags | VM_SOCK_CREATE_FLAGS_TCP |
73
+                                   VM_SOCK_CREATE_FLAGS_IPV4,
74
+@@ -103,7 +137,7 @@ VmRESTInitProtocolServer(
75
+ #ifdef AF_INET6
76
+     /**** Handle IPv6 case ****/
77
+ 
78
+-    dwError = VmwSockOpenServer(
79
++    dwError = VmwSockStartServer(
80
+                    pRESTHandle,
81
+                    dwFlags | VM_SOCK_CREATE_FLAGS_TCP |
82
+                           VM_SOCK_CREATE_FLAGS_IPV6,
83
+@@ -122,7 +156,7 @@ VmRESTInitProtocolServer(
84
+                   );
85
+     BAIL_ON_VMREST_ERROR(dwError);
86
+ 
87
+-    dwError = VmwSockEventQueueAdd(
88
++    dwError = VmwSockAddEventToQueueInLock(
89
+                   pRESTHandle,
90
+                   pSockContext->pEventQueue,
91
+                   pSockContext->pListenerTCP
92
+@@ -132,7 +166,7 @@ VmRESTInitProtocolServer(
93
+ #ifdef AF_INET6
94
+     if (!bNoIpV6)
95
+     {
96
+-        dwError = VmwSockEventQueueAdd(
97
++        dwError = VmwSockAddEventToQueueInLock(
98
+                       pRESTHandle,
99
+                       pSockContext->pEventQueue,
100
+                       pSockContext->pListenerTCP6
101
+@@ -194,7 +228,7 @@ VmRESTInitProtocolServer(
102
+ }
103
+ 
104
+ uint32_t
105
+-VmRESTShutdownProtocolServer(
106
++VmRESTStopProtocolServer(
107
+     PVMREST_HANDLE                   pRESTHandle,
108
+     uint32_t                         waitSecond
109
+     )
110
+@@ -221,6 +255,18 @@ VmRESTShutdownProtocolServer(
111
+ 
112
+ }
113
+ 
114
++void
115
++VmRESTShutdownProtocolServer(
116
++    PVMREST_HANDLE                   pRESTHandle
117
++    )
118
++{
119
++    if (pRESTHandle)
120
++    {
121
++        VmwSockShutdown(pRESTHandle);
122
++        VmRESTLogTerminate(pRESTHandle);
123
++    }
124
++}
125
++
126
+ static
127
+ PVOID
128
+ VmRESTSockWorkerThreadProc(
129
+@@ -569,6 +615,34 @@ VmRESTSockContextFree(
130
+ {
131
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
132
+ 
133
++    if (!pRESTHandle || !pSockContext)
134
++    {
135
++        dwError = ERROR_INVALID_PARAMETER;
136
++    }
137
++    BAIL_ON_VMREST_ERROR(dwError);
138
++
139
++
140
++    if (pSockContext->pListenerTCP)
141
++    {
142
++        dwError = VmwSockDeleteEventFromQueue(
143
++                      pRESTHandle,
144
++                      pSockContext->pEventQueue,
145
++                      pSockContext->pListenerTCP
146
++                      );
147
++        BAIL_ON_VMREST_ERROR(dwError);
148
++        VmwSockClose( pRESTHandle, pSockContext->pListenerTCP);
149
++    }
150
++    if (pSockContext->pListenerTCP6)
151
++    {
152
++        dwError = VmwSockDeleteEventFromQueue(
153
++                      pRESTHandle,
154
++                      pSockContext->pEventQueue,
155
++                      pSockContext->pListenerTCP6
156
++                      );
157
++        BAIL_ON_VMREST_ERROR(dwError);
158
++        VmwSockClose( pRESTHandle, pSockContext->pListenerTCP6);
159
++    }
160
++
161
+     if (pSockContext->pEventQueue)
162
+     {
163
+         dwError = VmwSockCloseEventQueue(pRESTHandle, pSockContext->pEventQueue, waitSecond);
164
+@@ -577,14 +651,14 @@ VmRESTSockContextFree(
165
+ 
166
+     if (pSockContext->pListenerTCP)
167
+     {
168
+-        VmwSockClose( pRESTHandle, pSockContext->pListenerTCP);
169
+         VmwSockRelease( pRESTHandle, pSockContext->pListenerTCP);
170
+     }
171
++
172
+     if (pSockContext->pListenerTCP6)
173
+     {
174
+-        VmwSockClose( pRESTHandle, pSockContext->pListenerTCP6);
175
+         VmwSockRelease( pRESTHandle, pSockContext->pListenerTCP6);
176
+     }
177
++
178
+     if (pSockContext->pWorkerThreads)
179
+     {
180
+         DWORD iThr = 0;
181
+diff --git a/include/public/vmrest.h b/include/public/vmrest.h
182
+index 5219952..6ebe3fd 100644
183
+--- a/include/public/vmrest.h
184
+@@ -46,6 +46,7 @@
185
+ #define     REST_ERROR_BAD_CONFIG_FILE_PATH                110
186
+ #define     REST_ERROR_PREV_INSTANCE_NOT_CLEAN             111
187
+ #define     REST_ERROR_INVALID_HANDLER                     112
188
++#define     REST_ENGINE_ERROR_INVALID_PARAM                112
189
+ #define     REST_ENGINE_SSL_CONFIG_FILE                    113
190
+ #define     REST_ENGINE_NO_DEBUG_LOGGING                   114
191
+ #define     REST_ENGINE_BAD_LOG_LEVEL                      115
192
+diff --git a/include/vmrestcommon.h b/include/vmrestcommon.h
193
+index d966f0e..3b7f17c 100644
194
+--- a/include/vmrestcommon.h
195
+@@ -260,14 +260,14 @@ typedef struct _REST_ENG_GLOBALS *PREST_ENG_GLOBALS;
196
+ typedef struct _VMREST_HANDLE
197
+ {
198
+     int                              debugLogLevel;
199
++    int                              instanceState;
200
+     FILE*                            logFile;
201
+     PVM_SOCK_PACKAGE                 pPackage;
202
+     PVM_SOCK_SSL_INFO                pSSLInfo;
203
+     PREST_PROCESSOR                  pHttpHandler;
204
+     PREST_ENG_GLOBALS                pInstanceGlobal;
205
+     PVMREST_SOCK_CONTEXT             pSockContext;
206
+-    PVM_REST_CONFIG                  pRESTConfig;                     
207
+-
208
++    PVM_REST_CONFIG                  pRESTConfig;
209
+ } VMREST_HANDLE;
210
+ 
211
+ typedef struct _VM_WORKER_THREAD_DATA
212
+@@ -296,11 +296,21 @@ VmRESTInitProtocolServer(
213
+     );
214
+ 
215
+ DWORD
216
+-VmRESTShutdownProtocolServer(
217
++VmRESTStartProtocolServer(
218
++    PVMREST_HANDLE                   pRESTHandle
219
++    );
220
++
221
++DWORD
222
++VmRESTStopProtocolServer(
223
+     PVMREST_HANDLE                   pRESTHandle,
224
+     uint32_t                         waitSecond
225
+     );
226
+ 
227
++VOID
228
++VmRESTShutdownProtocolServer(
229
++    PVMREST_HANDLE                   pRESTHandle
230
++    );
231
++
232
+ uint32_t
233
+ VmRESTCommonWriteDataAtOnce(
234
+     PVMREST_HANDLE                   pRESTHandle,
235
+diff --git a/include/vmsock.h b/include/vmsock.h
236
+index 7b60b29..5075aa7 100644
237
+--- a/include/vmsock.h
238
+@@ -87,7 +87,7 @@ VmwSockInitialize(
239
+     );
240
+ 
241
+ /**
242
+- * @brief Opens a server socket
243
++ * @brief Starts a server socket
244
+  *
245
+  * @param[in] Handle to library instance.
246
+  * @param[in]  dwFlags 32 bit flags defining socket creation preferences
247
+@@ -96,7 +96,7 @@ VmwSockInitialize(
248
+  * @return 0 on success
249
+  */
250
+ DWORD
251
+-VmwSockOpenServer(
252
++VmwSockStartServer(
253
+     PVMREST_HANDLE                   pRESTHandle,
254
+     VM_SOCK_CREATE_FLAGS             dwFlags,
255
+     PVM_SOCKET*                      ppSocket
256
+@@ -117,7 +117,7 @@ VmwSockCreateEventQueue(
257
+     );
258
+ 
259
+ /**
260
+- * @brief Adds a socket to the event queue
261
++ * @brief Add a socket to the event queue
262
+  *
263
+  * @param[in] Handle to library instance.
264
+  * @param[in] pQueue  Pointer to Event queue
265
+@@ -126,7 +126,23 @@ VmwSockCreateEventQueue(
266
+  * @return 0 on success
267
+  */
268
+ DWORD
269
+-VmwSockEventQueueAdd(
270
++VmwSockAddEventToQueueInLock(
271
++    PVMREST_HANDLE                   pRESTHandle,
272
++    PVM_SOCK_EVENT_QUEUE             pQueue,
273
++    PVM_SOCKET                       pSocket
274
++    );
275
++
276
++/**
277
++ * @brief Deletes a socket from the event queue
278
++ *
279
++ * @param[in] Handle to library instance.
280
++ * @param[in] pQueue  Pointer to Event queue
281
++ * @param[in] pSocket Pointer to Socket
282
++ *
283
++ * @return 0 on success
284
++ */
285
++DWORD
286
++VmwSockDeleteEventFromQueue(
287
+     PVMREST_HANDLE                   pRESTHandle,
288
+     PVM_SOCK_EVENT_QUEUE             pQueue,
289
+     PVM_SOCKET                       pSocket
290
+@@ -280,7 +296,7 @@ typedef enum
291
+     VM_SOCK_TYPE_TCP_V6
292
+ } VM_SOCK_TYPE;
293
+ 
294
+-typedef DWORD (*PFN_OPEN_SERVER_SOCKET)(
295
++typedef DWORD (*PFN_START_SERVER_SOCKET)(
296
+                     PVMREST_HANDLE       pRESTHandle,
297
+                     VM_SOCK_CREATE_FLAGS dwFlags,
298
+                     PVM_SOCKET*          ppSocket
299
+@@ -291,7 +307,13 @@ typedef DWORD (*PFN_CREATE_EVENT_QUEUE)(
300
+                     PVM_SOCK_EVENT_QUEUE* ppQueue
301
+                     );
302
+ 
303
+-typedef DWORD (*PFN_ADD_EVENT_QUEUE)(
304
++typedef DWORD (*PFN_ADD_EVENT_TO_QUEUE)(
305
++                    PVMREST_HANDLE       pRESTHandle,
306
++                    PVM_SOCK_EVENT_QUEUE pQueue,
307
++                    PVM_SOCKET           pSocket
308
++                    );
309
++
310
++typedef DWORD (*PFN_DELETE_EVENT_FROM_QUEUE)(
311
+                     PVMREST_HANDLE       pRESTHandle,
312
+                     PVM_SOCK_EVENT_QUEUE pQueue,
313
+                     PVM_SOCKET           pSocket
314
+@@ -359,9 +381,10 @@ typedef DWORD(*PFN_GET_PEER_INFO)(
315
+ 
316
+ typedef struct _VM_SOCK_PACKAGE
317
+ {
318
+-    PFN_OPEN_SERVER_SOCKET              pfnOpenServerSocket;
319
++    PFN_START_SERVER_SOCKET             pfnStartServerSocket;
320
+     PFN_CREATE_EVENT_QUEUE              pfnCreateEventQueue;
321
+-    PFN_ADD_EVENT_QUEUE                 pfnAddEventQueue;
322
++    PFN_ADD_EVENT_TO_QUEUE              pfnAddEventToQueue;
323
++    PFN_DELETE_EVENT_FROM_QUEUE         pfnDeleteEventFromQueue;
324
+     PFN_WAIT_FOR_EVENT                  pfnWaitForEvent;
325
+     PFN_CLOSE_EVENT_QUEUE               pfnCloseEventQueue;
326
+     PFN_READ                            pfnRead;
327
+diff --git a/server/restengine/defines.h b/server/restengine/defines.h
328
+index 380af9c..ec1e542 100644
329
+--- a/server/restengine/defines.h
330
+@@ -17,11 +17,12 @@ typedef void* (PFN_VMREST_THR_ROUTINE)(void*);
331
+ 
332
+ /* HTTP protocol header defines */
333
+ 
334
+-#define MAX_METHOD_LEN             10
335
+-#define MAX_URI_LEN                10240
336
++#define MAX_METHOD_LEN              10
337
++#define MAX_URI_LEN                 10240
338
+ #define MAX_VERSION_LEN             9
339
+ #define MAX_STATUS_LEN              4
340
+-#define MAX_REA_PHRASE_LEN         32
341
++#define MAX_REA_PHRASE_LEN          32
342
++#define MAX_STOP_WAIT_SECONDS       600
343
+ #define HTTP_VER_LEN                8
344
+ #define HTTP_CHUNK_DATA_MIN_LEN     3
345
+ #define HTTP_CHUNKED_DATA_LEN       8
346
+@@ -62,6 +63,15 @@ typedef enum _HTTP_PAYLOAD_TYPE
347
+     HTTP_PAYLOAD_TRANSFER_ENCODING
348
+ }HTTP_PAYLOAD_TYPE;
349
+ 
350
++typedef enum _VM_REST_INSTANCE_STATE
351
++{
352
++    VMREST_INSTANCE_UNINITIALIZED      = -1,
353
++    VMREST_INSTANCE_INITIALIZED        = 1,
354
++    VMREST_INSTANCE_STARTED            = 2,
355
++    VMREST_INSTANCE_STOPPED            = 3,
356
++    VMREST_INSTANCE_SHUTDOWN           = 4
357
++}VM_REST_INSTANCE_STATE;
358
++
359
+ typedef enum _VM_REST_PROCESSING_STATE
360
+ {
361
+     PROCESS_INVALID              = -1,
362
+diff --git a/server/restengine/httpAllocStruct.c b/server/restengine/httpAllocStruct.c
363
+index 27aa9ed..18a016e 100644
364
+--- a/server/restengine/httpAllocStruct.c
365
+@@ -345,6 +345,8 @@ VmRESTAllocateHandle(
366
+ 
367
+     pRESTHandle->pHttpHandler = NULL;
368
+ 
369
++    pRESTHandle->instanceState = VMREST_INSTANCE_UNINITIALIZED;
370
++
371
+     *ppRESTHandle = pRESTHandle;
372
+ 
373
+ cleanup:
374
+diff --git a/server/restengine/httpMain.c b/server/restengine/httpMain.c
375
+index 562a555..9f3afcd 100644
376
+--- a/server/restengine/httpMain.c
377
+@@ -23,7 +23,6 @@ VmHTTPInit(
378
+ 
379
+     if (!pRESTHandle || !pConfig)
380
+     {
381
+-        VMREST_LOG_DEBUG(pRESTHandle,"%s","Invalid params");
382
+         dwError = REST_ERROR_INVALID_CONFIG;
383
+     }
384
+     BAIL_ON_VMREST_ERROR(dwError);
385
+@@ -41,21 +40,19 @@ VmHTTPInit(
386
+                   );
387
+     BAIL_ON_VMREST_ERROR(dwError);
388
+ 
389
+-    pRESTHandle->debugLogLevel = pRESTHandle->pRESTConfig->debugLogLevel;
390
+-
391
+-    /**** Init the debug log ****/
392
+-    dwError = VmRESTLogInitialize(
393
++    /**** Init logging and transport ****/
394
++    dwError = VmRESTInitProtocolServer(
395
+                   pRESTHandle
396
+                   );
397
+     BAIL_ON_VMREST_ERROR(dwError);
398
+ 
399
+-    /**** Init Transport ****/
400
+-    dwError = VmwSockInitialize(pRESTHandle);
401
+-    BAIL_ON_VMREST_ERROR(dwError);
402
++    pRESTHandle->debugLogLevel = pRESTHandle->pRESTConfig->debugLogLevel;
403
+ 
404
+     /**** Update context Info for this lib instance ****/
405
+     pRESTHandle->pInstanceGlobal->useEndPoint = 0;
406
+ 
407
++    VMREST_LOG_INFO(pRESTHandle,"%s","C-REST_ENGINE: Library initialized ...");
408
++
409
+ cleanup:
410
+ 
411
+     return dwError;
412
+@@ -72,9 +69,13 @@ VmHTTPStart(
413
+ {
414
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
415
+ 
416
+-    dwError = VmRESTInitProtocolServer(pRESTHandle);
417
++    dwError = VmRESTStartProtocolServer(
418
++                  pRESTHandle
419
++                  );
420
+     BAIL_ON_VMREST_ERROR(dwError);
421
+ 
422
++    VMREST_LOG_INFO(pRESTHandle,"%s","C-REST_ENGINE: Library started ...");
423
++
424
+ cleanup:
425
+ 
426
+     return dwError;
427
+@@ -132,14 +133,22 @@ VmHTTPStop(
428
+ {
429
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
430
+ 
431
+-    VMREST_LOG_DEBUG(pRESTHandle,"%s","Shutting down rest engine ....");
432
+-    dwError = VmRESTShutdownProtocolServer(pRESTHandle, waitSecond);
433
++    VMREST_LOG_INFO(pRESTHandle,"%s","C-REST_ENGINE: Stopping library ... No more requests will be accepted");
434
++
435
++    dwError = VmRESTStopProtocolServer(
436
++                  pRESTHandle, 
437
++                  waitSecond
438
++                  );
439
+     BAIL_ON_VMREST_ERROR(dwError);
440
+ 
441
++    VMREST_LOG_INFO(pRESTHandle,"%s", "C-REST-ENGINE: Library stopped ...");
442
++
443
+ cleanup:
444
+-    VMREST_LOG_DEBUG(pRESTHandle,"Stop returning %u", dwError);
445
++
446
+     return dwError;
447
+ error:
448
++
449
++    VMREST_LOG_ERROR(pRESTHandle,"C-REST-ENGINE: Library stop failed ... Do not attempt shutdown, dwError = %u", dwError);
450
+     goto cleanup;
451
+ }
452
+ 
453
+@@ -148,11 +157,13 @@ VmHTTPShutdown(
454
+     PVMREST_HANDLE                  pRESTHandle
455
+     )
456
+ {
457
+-    VmwSockShutdown(pRESTHandle);
458
++    VMREST_LOG_INFO(pRESTHandle,"%s","C-REST-ENGINE: Shutting down Library");
459
++    VmRESTShutdownProtocolServer(
460
++        pRESTHandle
461
++        );
462
+ 
463
+     if (pRESTHandle)
464
+     {
465
+-        VmRESTLogTerminate(pRESTHandle);
466
+         VmRESTFreeHandle(pRESTHandle);        
467
+     }
468
+ }
469
+diff --git a/server/restengine/libmain.c b/server/restengine/libmain.c
470
+index a7b85a1..3992b76 100644
471
+--- a/server/restengine/libmain.c
472
+@@ -48,9 +48,9 @@ VmRESTInit(
473
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
474
+     PVMREST_HANDLE                   pRESTHandle = NULL;
475
+ 
476
+-    if (!ppRESTHandle)
477
++    if (!pConfig || !ppRESTHandle)
478
+     {
479
+-        dwError = REST_ERROR_INVALID_HANDLER;
480
++        dwError = REST_ENGINE_ERROR_INVALID_PARAM;
481
+     }
482
+     BAIL_ON_VMREST_ERROR(dwError);
483
+ 
484
+@@ -69,6 +69,8 @@ VmRESTInit(
485
+                   );
486
+     BAIL_ON_VMREST_ERROR(dwError);
487
+ 
488
++    pRESTHandle->instanceState = VMREST_INSTANCE_INITIALIZED;
489
++
490
+     *ppRESTHandle = pRESTHandle;
491
+ 
492
+ cleanup:
493
+@@ -102,7 +104,7 @@ VmRESTSetSSLInfo(
494
+ 
495
+     if (!pRESTHandle || !pDataBuffer || (bufferSize == 0) || (bufferSize > MAX_SSL_DATA_BUF_LEN) || (sslDataType < SSL_DATA_TYPE_KEY) || (sslDataType > SSL_DATA_TYPE_CERT))
496
+     {
497
+-        dwError = REST_ERROR_INVALID_HANDLER;
498
++        dwError = REST_ENGINE_ERROR_INVALID_PARAM;
499
+     }
500
+     BAIL_ON_VMREST_ERROR(dwError);
501
+ 
502
+@@ -177,9 +179,9 @@ VmRESTStart(
503
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
504
+     int                              ret = 0;
505
+ 
506
+-    if (!pRESTHandle)
507
++    if (!pRESTHandle || (pRESTHandle->instanceState != VMREST_INSTANCE_INITIALIZED))
508
+     {
509
+-        dwError = REST_ERROR_INVALID_REST_PROCESSER;
510
++        dwError = REST_ENGINE_ERROR_INVALID_PARAM;
511
+     }
512
+     BAIL_ON_VMREST_ERROR(dwError);
513
+ 
514
+@@ -218,9 +220,14 @@ VmRESTStart(
515
+         BAIL_ON_VMREST_ERROR(dwError);
516
+     }
517
+ 
518
++    pRESTHandle->instanceState = VMREST_INSTANCE_STARTED;
519
++
520
+ cleanup:
521
++
522
+     return dwError;
523
++
524
+ error:
525
++
526
+     goto cleanup;
527
+ 
528
+ }
529
+@@ -236,9 +243,9 @@ VmRESTRegisterHandler(
530
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
531
+     PREST_PROCESSOR                  pzHandler = NULL;
532
+ 
533
+-    if (!pHandler || !pRESTHandle)
534
++    if (!pHandler || !pRESTHandle || (pRESTHandle->instanceState != VMREST_INSTANCE_INITIALIZED))
535
+     {
536
+-        dwError = REST_ERROR_INVALID_REST_PROCESSER;
537
++        dwError = REST_ENGINE_ERROR_INVALID_PARAM;
538
+     }
539
+     BAIL_ON_VMREST_ERROR(dwError);
540
+ 
541
+@@ -292,6 +299,12 @@ VmRESTFindEndpoint(
542
+     PREST_ENDPOINT                   temp = NULL;
543
+     PREST_ENDPOINT                   pEndPoint = NULL;
544
+ 
545
++    if (!pRESTHandle || !pszEndpoint || !ppEndpoint || (pRESTHandle->instanceState == VMREST_INSTANCE_UNINITIALIZED) || (pRESTHandle->instanceState == VMREST_INSTANCE_SHUTDOWN))
546
++    {
547
++        dwError = REST_ENGINE_ERROR_INVALID_PARAM;
548
++    }
549
++    BAIL_ON_VMREST_ERROR(dwError);
550
++
551
+     dwError = VmRestEngineGetEndPoint(
552
+                   pRESTHandle,
553
+                   (char*)pszEndpoint,
554
+@@ -329,12 +342,18 @@ VmRESTFindEndpoint(
555
+ uint32_t
556
+ VmRESTUnRegisterHandler(
557
+     PVMREST_HANDLE                   pRESTHandle,
558
+-    char const*                      pzEndPointURI
559
++    char const*                      pcszEndPointURI
560
+     )
561
+ {
562
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
563
+ 
564
+-    if (!pzEndPointURI)
565
++    if (!pRESTHandle || !pcszEndPointURI || (pRESTHandle->instanceState != VMREST_INSTANCE_STOPPED))
566
++    {
567
++        dwError = REST_ENGINE_ERROR_INVALID_PARAM;
568
++    }
569
++    BAIL_ON_VMREST_ERROR(dwError);
570
++
571
++    if (!pcszEndPointURI)
572
+     {
573
+         /**** Interacting with HTTP directly ****/
574
+         dwError = VmHTTPUnRegisterHandler(
575
+@@ -346,7 +365,7 @@ VmRESTUnRegisterHandler(
576
+         /**** Endpoint based library instance ****/
577
+         dwError = VmRestEngineRemoveEndpoint(
578
+                       pRESTHandle,
579
+-                      pzEndPointURI
580
++                      pcszEndPointURI
581
+                       );
582
+     }
583
+     BAIL_ON_VMREST_ERROR(dwError);
584
+@@ -365,12 +384,14 @@ VmRESTStop(
585
+ {
586
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
587
+ 
588
+-    if (!pRESTHandle)
589
++    if (!pRESTHandle || (waitSeconds > MAX_STOP_WAIT_SECONDS) || (pRESTHandle->instanceState != VMREST_INSTANCE_STARTED))
590
+     {  
591
+-        dwError = REST_ERROR_INVALID_HANDLER;
592
++        dwError = REST_ENGINE_ERROR_INVALID_PARAM;
593
+     }
594
+     BAIL_ON_VMREST_ERROR(dwError);
595
+ 
596
++    pRESTHandle->instanceState = VMREST_INSTANCE_STOPPED;
597
++
598
+     dwError = VmHTTPStop(
599
+                   pRESTHandle,
600
+                   waitSeconds
601
+@@ -388,14 +409,18 @@ VmRESTShutdown(
602
+     PVMREST_HANDLE                   pRESTHandle
603
+     )
604
+ {
605
+-    if (pRESTHandle->pInstanceGlobal->useEndPoint == 1)
606
++    if (pRESTHandle && (pRESTHandle->instanceState == VMREST_INSTANCE_STOPPED))
607
+     {
608
+-        VmRestEngineShutdownEndPointRegistration(
609
+-            pRESTHandle
610
+-            );
611
++        pRESTHandle->instanceState = VMREST_INSTANCE_SHUTDOWN;
612
++        if (pRESTHandle->pInstanceGlobal->useEndPoint == 1)
613
++        {
614
++            VmRestEngineShutdownEndPointRegistration(
615
++                pRESTHandle
616
++                );
617
++        }
618
++        
619
++        VmHTTPShutdown(pRESTHandle);
620
+     }
621
+-
622
+-    VmHTTPShutdown(pRESTHandle);
623
+ }
624
+ 
625
+ uint32_t
626
+@@ -408,14 +433,28 @@ VmRESTGetData(
627
+ {
628
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
629
+ 
630
++    if (!pRESTHandle || (pRESTHandle->instanceState != VMREST_INSTANCE_STARTED))
631
++    {
632
++        dwError = REST_ENGINE_ERROR_INVALID_PARAM;
633
++    }
634
++    BAIL_ON_VMREST_ERROR(dwError);
635
++
636
+     dwError = VmRESTGetHttpPayload(
637
+                   pRESTHandle,
638
+                   pRequest,
639
+                   pBuffer,
640
+                   bytesRead
641
+                   );
642
++    BAIL_ON_VMREST_ERROR(dwError);
643
+                   
644
++cleanup:
645
++
646
+     return dwError;
647
++
648
++error:
649
++
650
++    goto cleanup;
651
++
652
+ }
653
+ 
654
+ /*** GetData Zero copy API ****/
655
+@@ -427,14 +466,12 @@ VmRESTGetDataZC(
656
+     uint32_t*                        nBytes
657
+     )
658
+ {
659
+-
660
+-
661
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
662
+ 
663
+-    if (!pRESTHandle || !pRequest || !ppBuffer  || !nBytes)
664
++    if (!pRESTHandle || !pRequest || !ppBuffer  || !nBytes || (pRESTHandle->instanceState != VMREST_INSTANCE_STARTED))
665
+     {
666
+         VMREST_LOG_ERROR(pRESTHandle,"%s","Invalid params");
667
+-        dwError = VMREST_HTTP_INVALID_PARAMS;
668
++        dwError = REST_ENGINE_ERROR_INVALID_PARAM;
669
+     }
670
+     BAIL_ON_VMREST_ERROR(dwError);
671
+ 
672
+@@ -463,23 +500,38 @@ uint32_t
673
+ VmRESTSetData(
674
+     PVMREST_HANDLE                   pRESTHandle,
675
+     PREST_RESPONSE*                  ppResponse,
676
+-    char const*                      buffer,
677
++    char const*                      pcszBuffer,
678
+     uint32_t                         dataLen,
679
+     uint32_t*                        bytesWritten
680
+     )
681
+ {
682
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
683
+ 
684
++
685
++    if (!pRESTHandle || !ppResponse || !pcszBuffer || !bytesWritten || (pRESTHandle->instanceState != VMREST_INSTANCE_STARTED))
686
++    {
687
++        VMREST_LOG_ERROR(pRESTHandle,"%s","Invalid params");
688
++        dwError = REST_ENGINE_ERROR_INVALID_PARAM;
689
++    }
690
++    BAIL_ON_VMREST_ERROR(dwError);
691
++
692
+     dwError = VmRESTSetHttpPayload(
693
+                   pRESTHandle,
694
+                   ppResponse,
695
+-                  buffer,
696
++                  pcszBuffer,
697
+                   dataLen,
698
+                   bytesWritten
699
+                   );
700
++    BAIL_ON_VMREST_ERROR(dwError);
701
++
702
++cleanup:
703
+ 
704
+     return dwError;
705
+ 
706
++error:
707
++
708
++    goto cleanup;
709
++
710
+ }
711
+ 
712
+ /**** SetData Zero Copy API ****/
713
+@@ -487,21 +539,37 @@ uint32_t
714
+ VmRESTSetDataZC(
715
+     PVMREST_HANDLE                   pRESTHandle,
716
+     PREST_RESPONSE*                  ppResponse,
717
+-    char const*                      pBuffer,
718
++    char const*                      pcszBuffer,
719
+     uint32_t                         nBytes
720
+     )
721
+ {
722
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
723
+ 
724
++
725
++    if (!pRESTHandle || !ppResponse || !pcszBuffer || (pRESTHandle->instanceState != VMREST_INSTANCE_STARTED))
726
++    {
727
++        VMREST_LOG_ERROR(pRESTHandle,"%s","Invalid params");
728
++        dwError = REST_ENGINE_ERROR_INVALID_PARAM;
729
++    }
730
++    BAIL_ON_VMREST_ERROR(dwError);
731
++
732
++
733
+     dwError = VmRESTSetHttpPayloadZeroCopy(
734
+                   pRESTHandle,
735
+                   ppResponse,
736
+-                  pBuffer,
737
++                  pcszBuffer,
738
+                   nBytes
739
+                   );
740
++    BAIL_ON_VMREST_ERROR(dwError);
741
++
742
++cleanup:
743
+ 
744
+     return dwError;
745
+ 
746
++error:
747
++
748
++    goto cleanup;
749
++
750
+ }
751
+ 
752
+ uint32_t
753
+@@ -513,6 +581,12 @@ VmRESTSetSuccessResponse(
754
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
755
+     char*                            connection = NULL;
756
+ 
757
++    if (!pRequest || !ppResponse)
758
++    {
759
++        dwError = REST_ENGINE_ERROR_INVALID_PARAM;
760
++    }
761
++    BAIL_ON_VMREST_ERROR(dwError);
762
++
763
+     dwError = VmRESTSetHttpStatusCode(
764
+                   ppResponse,
765
+                   "200"
766
+@@ -571,8 +645,8 @@ VmRESTSetSuccessResponse(
767
+ uint32_t
768
+ VmRESTSetFailureResponse(
769
+     PREST_RESPONSE*                  ppResponse,
770
+-    char const*                      pErrorCode,
771
+-    char const*                      pErrorMessage
772
++    char const*                      pcszErrorCode,
773
++    char const*                      pcszErrorMessage
774
+     )
775
+ {   
776
+     uint32_t                         dwError = REST_ENGINE_SUCCESS;
777
+@@ -584,22 +658,22 @@ VmRESTSetFailureResponse(
778
+ 
779
+     /**** If error code and message is not provided, send internal server error ****/
780
+ 
781
+-    if (!pErrorCode)
782
++    if (!pcszErrorCode)
783
+     {
784
+         strcpy(errorCode,"500");
785
+     }
786
+-    else if ((strlen(pErrorCode) > 0) && (strlen(pErrorCode) <= MAX_STATUS_LEN))
787
++    else if ((strlen(pcszErrorCode) > 0) && (strlen(pcszErrorCode) <= MAX_STATUS_LEN))
788
+     {
789
+-        strcpy(errorCode, pErrorCode);
790
++        strcpy(errorCode, pcszErrorCode);
791
+     }
792
+ 
793
+-    if (!pErrorMessage)
794
++    if (!pcszErrorMessage)
795
+     {
796
+         strcpy(errorMessage, "Internal Server Error");
797
+     }
798
+-    else if ((strlen(pErrorMessage) > 0) && (strlen(pErrorMessage) <= MAX_REA_PHRASE_LEN))
799
++    else if ((strlen(pcszErrorMessage) > 0) && (strlen(pcszErrorMessage) <= MAX_REA_PHRASE_LEN))
800
+     {
801
+-        strcpy(errorMessage, pErrorMessage);
802
++        strcpy(errorMessage, pcszErrorMessage);
803
+     }
804
+ 
805
+     dwError = VmRESTSetHttpStatusCode(
806
+diff --git a/transport/api/api.c b/transport/api/api.c
807
+index c48de46..cc2c930 100644
808
+--- a/transport/api/api.c
809
+@@ -19,7 +19,7 @@
810
+ #endif
811
+ 
812
+ DWORD
813
+-VmwSockOpenServer(
814
++VmwSockStartServer(
815
+     PVMREST_HANDLE                   pRESTHandle,
816
+     VM_SOCK_CREATE_FLAGS             dwFlags,
817
+     PVM_SOCKET*                      ppSocket
818
+@@ -33,7 +33,7 @@ VmwSockOpenServer(
819
+         BAIL_ON_VMSOCK_ERROR(dwError);
820
+     }
821
+ 
822
+-    dwError = pRESTHandle->pPackage->pfnOpenServerSocket(
823
++    dwError = pRESTHandle->pPackage->pfnStartServerSocket(
824
+                                      pRESTHandle,
825
+                                      dwFlags,
826
+                                      ppSocket);
827
+@@ -64,7 +64,7 @@ VmwSockCreateEventQueue(
828
+ }
829
+ 
830
+ DWORD
831
+-VmwSockEventQueueAdd(
832
++VmwSockAddEventToQueueInLock(
833
+     PVMREST_HANDLE                   pRESTHandle,
834
+     PVM_SOCK_EVENT_QUEUE             pQueue,
835
+     PVM_SOCKET                       pSocket
836
+@@ -78,13 +78,36 @@ VmwSockEventQueueAdd(
837
+         BAIL_ON_VMSOCK_ERROR(dwError);
838
+     }
839
+ 
840
+-    dwError = pRESTHandle->pPackage->pfnAddEventQueue(pRESTHandle,pQueue, pSocket);
841
++    dwError = pRESTHandle->pPackage->pfnAddEventToQueue(pRESTHandle,pQueue, pSocket);
842
+ 
843
+ error:
844
+ 
845
+     return dwError;
846
+ }
847
+ 
848
++DWORD
849
++VmwSockDeleteEventFromQueue(
850
++    PVMREST_HANDLE                   pRESTHandle,
851
++    PVM_SOCK_EVENT_QUEUE             pQueue,
852
++    PVM_SOCKET                       pSocket
853
++    )
854
++{
855
++    DWORD                            dwError = REST_ENGINE_SUCCESS;
856
++
857
++    if (!pQueue || !pSocket || !pRESTHandle)
858
++    {
859
++        dwError = ERROR_INVALID_PARAMETER;
860
++        BAIL_ON_VMSOCK_ERROR(dwError);
861
++    }
862
++
863
++    dwError = pRESTHandle->pPackage->pfnDeleteEventFromQueue(pRESTHandle,pQueue, pSocket);
864
++
865
++error:
866
++
867
++    return dwError;
868
++}
869
++
870
++
871
+ DWORD
872
+ VmwSockWaitForEvent(
873
+     PVMREST_HANDLE                   pRESTHandle,
874
+diff --git a/transport/posix/libmain.c b/transport/posix/libmain.c
875
+index e7a5056..67c6023 100644
876
+--- a/transport/posix/libmain.c
877
+@@ -47,9 +47,10 @@ VmRESTGetSockPackagePosix(
878
+ 
879
+     pSockPackagePosix = *ppSockPackagePosix;
880
+ 
881
+-    pSockPackagePosix->pfnOpenServerSocket = &VmSockPosixOpenServer;
882
++    pSockPackagePosix->pfnStartServerSocket = &VmSockPosixStartServer;
883
+     pSockPackagePosix->pfnCreateEventQueue = &VmSockPosixCreateEventQueue;
884
+-    pSockPackagePosix->pfnAddEventQueue = &VmSockPosixEventQueueAdd;
885
++    pSockPackagePosix->pfnAddEventToQueue = &VmSockPosixAddEventToQueueInLock;
886
++    pSockPackagePosix->pfnDeleteEventFromQueue = &VmSockPosixDeleteEventFromQueue;
887
+     pSockPackagePosix->pfnWaitForEvent = &VmSockPosixWaitForEvent;
888
+     pSockPackagePosix->pfnCloseEventQueue = &VmSockPosixCloseEventQueue;
889
+     pSockPackagePosix->pfnRead = &VmSockPosixRead;
890
+diff --git a/transport/posix/prototypes.h b/transport/posix/prototypes.h
891
+index 4fa664c..d60e5e2 100644
892
+--- a/transport/posix/prototypes.h
893
+@@ -12,7 +12,7 @@
894
+ */
895
+ 
896
+ DWORD
897
+-VmSockPosixOpenServer(
898
++VmSockPosixStartServer(
899
+     PVMREST_HANDLE                   pRESTHandle,
900
+     VM_SOCK_CREATE_FLAGS             dwFlags,
901
+     PVM_SOCKET*                      ppSocket
902
+@@ -25,7 +25,14 @@ VmSockPosixCreateEventQueue(
903
+     );
904
+ 
905
+ DWORD
906
+-VmSockPosixEventQueueAdd(
907
++VmSockPosixAddEventToQueueInLock(
908
++    PVMREST_HANDLE                   pRESTHandle,
909
++    PVM_SOCK_EVENT_QUEUE             pQueue,
910
++    PVM_SOCKET                       pSocket
911
++    );
912
++
913
++DWORD
914
++VmSockPosixDeleteEventFromQueue(
915
+     PVMREST_HANDLE                   pRESTHandle,
916
+     PVM_SOCK_EVENT_QUEUE             pQueue,
917
+     PVM_SOCKET                       pSocket
918
+diff --git a/transport/posix/socket.c b/transport/posix/socket.c
919
+index ec7ed3d..b34ef15 100644
920
+--- a/transport/posix/socket.c
921
+@@ -22,19 +22,12 @@ VmSockPosixCreateSignalSockets(
922
+ 
923
+ static
924
+ DWORD
925
+-VmSockPosixEventQueueAdd_inlock(
926
++VmSockPosixAddEventToQueue(
927
+     PVM_SOCK_EVENT_QUEUE             pQueue,
928
+     BOOLEAN                          bOneShot,
929
+     PVM_SOCKET                       pSocket
930
+     );
931
+ 
932
+-static
933
+-DWORD
934
+-VmSockPosixEventQueueDelete_inlock(
935
+-    PVM_SOCK_EVENT_QUEUE             pQueue,
936
+-    PVM_SOCKET                       pSocket
937
+-    );
938
+-
939
+ static
940
+ DWORD
941
+ VmSockPosixAcceptConnection(
942
+@@ -99,7 +92,7 @@ VmRESTCreateSSLObject(
943
+ 
944
+ 
945
+ DWORD
946
+-VmSockPosixOpenServer(
947
++VmSockPosixStartServer(
948
+     PVMREST_HANDLE                   pRESTHandle,
949
+     VM_SOCK_CREATE_FLAGS             dwFlags,
950
+     PVM_SOCKET*                      ppSocket
951
+@@ -388,7 +381,7 @@ VmSockPosixCreateEventQueue(
952
+     pQueue->bShutdown = 0;
953
+     pQueue->thrCnt = pRESTHandle->pRESTConfig->nWorkerThr;
954
+ 
955
+-    dwError = VmSockPosixEventQueueAdd_inlock(
956
++    dwError = VmSockPosixAddEventToQueue(
957
+                   pQueue,
958
+                   FALSE,
959
+                   pQueue->pSignalReader
960
+@@ -420,7 +413,7 @@ VmSockPosixCreateEventQueue(
961
+ }
962
+ 
963
+ DWORD
964
+-VmSockPosixEventQueueAdd(
965
++VmSockPosixAddEventToQueueInLock(
966
+     PVMREST_HANDLE                   pRESTHandle,
967
+     PVM_SOCK_EVENT_QUEUE             pQueue,
968
+     PVM_SOCKET                       pSocket
969
+@@ -441,7 +434,7 @@ VmSockPosixEventQueueAdd(
970
+ 
971
+     bLocked = TRUE;
972
+ 
973
+-    dwError = VmSockPosixEventQueueAdd_inlock(
974
++    dwError = VmSockPosixAddEventToQueue(
975
+                   pQueue,
976
+                   FALSE,
977
+                   pSocket
978
+@@ -462,6 +455,39 @@ VmSockPosixEventQueueAdd(
979
+     goto cleanup;
980
+ }
981
+ 
982
++DWORD
983
++VmSockPosixDeleteEventFromQueue(
984
++    PVMREST_HANDLE                   pRESTHandle,
985
++    PVM_SOCK_EVENT_QUEUE             pQueue,
986
++    PVM_SOCKET                       pSocket
987
++    )
988
++{
989
++    DWORD                            dwError = REST_ENGINE_SUCCESS;
990
++    struct                           epoll_event event = {0};
991
++
992
++    if (!pSocket || !pQueue || !pRESTHandle)
993
++    {
994
++        dwError = REST_ERROR_INVALID_HANDLER;
995
++    }
996
++    BAIL_ON_VMREST_ERROR(dwError);
997
++
998
++    if (epoll_ctl(pQueue->epollFd, EPOLL_CTL_DEL, pSocket->fd, &event) < 0)
999
++    {
1000
++        dwError = VM_SOCK_POSIX_ERROR_SYS_CALL_FAILED;
1001
++        BAIL_ON_VMREST_ERROR(dwError);
1002
++    }
1003
++
1004
++cleanup:
1005
++
1006
++    return dwError;
1007
++
1008
++error:
1009
++
1010
++    goto cleanup;
1011
++
1012
++}
1013
++
1014
++
1015
+ DWORD
1016
+ VmSockPosixWaitForEvent(
1017
+     PVMREST_HANDLE                   pRESTHandle,
1018
+@@ -539,7 +565,8 @@ VmSockPosixWaitForEvent(
1019
+             if (pEvent->events & (EPOLLERR | EPOLLHUP))
1020
+             {
1021
+                 eventType = VM_SOCK_EVENT_TYPE_CONNECTION_CLOSED;
1022
+-                dwError = VmSockPosixEventQueueDelete_inlock(
1023
++                dwError = VmSockPosixDeleteEventFromQueue(
1024
++                              pRESTHandle,
1025
+                               pQueue,
1026
+                               pEventSocket
1027
+                               );
1028
+@@ -577,7 +604,7 @@ VmSockPosixWaitForEvent(
1029
+                 }
1030
+ 
1031
+                 /**** Start watching new connection ****/
1032
+-                dwError = VmSockPosixEventQueueAdd_inlock(
1033
++                dwError = VmSockPosixAddEventToQueue(
1034
+                               pQueue,
1035
+                               TRUE,
1036
+                               pSocket
1037
+@@ -639,6 +666,7 @@ VmSockPosixWaitForEvent(
1038
+                     if (bFound)
1039
+                     {
1040
+                         VMREST_LOG_DEBUG(pRESTHandle,"Action: DEFERRED, IO sock found in queue(Succeeding),  Io Sock %d, timer %d", pSocket->pIoSocket->fd, pSocket->fd );
1041
++                        pEventSocket->pIoSocket->pTimerSocket = NULL;
1042
+                     }
1043
+                     else
1044
+                     {
1045
+@@ -651,17 +679,30 @@ VmSockPosixWaitForEvent(
1046
+                         {
1047
+                              /**** We are good to close actual IO Socket here ****/
1048
+                              VMREST_LOG_INFO(pRESTHandle,"Action: IO DELETION, IoSocket %d, timer %d", pSocket->pIoSocket->fd, pSocket->fd );
1049
+-                             eventType = VM_SOCK_EVENT_TYPE_CONNECTION_TIMEOUT;
1050
+-                             pSocket = pSocket->pIoSocket;
1051
+-                             
1052
++
1053
++                             pSocket = pEventSocket->pIoSocket;
1054
+                              /**** Delete IO from queue ****/
1055
+-                             VmSockPosixEventQueueDelete_inlock(
1056
++                             VmSockPosixDeleteEventFromQueue(
1057
++                                           pRESTHandle,
1058
+                                            pQueue,
1059
+                                            pSocket
1060
+                                            );
1061
++
1062
++                             if ((pRESTHandle->pSSLInfo->isSecure) && (!(pSocket->bSSLHandShakeCompleted)))
1063
++                             {
1064
++                                 /**** SSL handshake is not completed, no response will be sent, free IoSocket ****/
1065
++                                 pEventSocket->pIoSocket = NULL;
1066
++                                 VmSockPosixCloseSocket(pRESTHandle,pSocket);
1067
++                                 VmSockPosixReleaseSocket(pRESTHandle,pSocket);
1068
++                                 eventType = VM_SOCK_EVENT_TYPE_UNKNOWN;
1069
++                             }
1070
++                             else
1071
++                             {
1072
++                                 eventType = VM_SOCK_EVENT_TYPE_CONNECTION_TIMEOUT;
1073
++                                 pEventSocket->pIoSocket->pTimerSocket = NULL;
1074
++                             }
1075
+                         }
1076
+                     }
1077
+-                    pEventSocket->pIoSocket->pTimerSocket = NULL;
1078
+                 }
1079
+ 
1080
+                 /** Close and free the timer socket ****/
1081
+@@ -747,7 +788,14 @@ VmSockPosixWaitForEvent(
1082
+ 
1083
+ error:
1084
+ 
1085
+-    VMREST_LOG_ERROR(pRESTHandle,"Error while processing socket event, dwError = %u", dwError);
1086
++    if ((dwError == ERROR_SHUTDOWN_IN_PROGRESS) && pQueue)
1087
++    {
1088
++        VMREST_LOG_INFO(pRESTHandle,"C-REST-ENGINE: Shutting down...Cleaning worker thread %d", (pQueue->thrCnt  + 1));
1089
++    }
1090
++    else
1091
++    {
1092
++        VMREST_LOG_ERROR(pRESTHandle,"Error while processing socket event, dwError = %u", dwError);
1093
++    }
1094
+     if (ppSocket)
1095
+     {
1096
+         *ppSocket = NULL;
1097
+@@ -987,10 +1035,11 @@ VmSockPosixRead(
1098
+ 
1099
+ error:
1100
+ 
1101
+-    if (pSocket && pRESTHandle->pSockContext)
1102
++    if (pSocket && pRESTHandle && pRESTHandle->pSockContext)
1103
+     {
1104
+         /**** Delete the socket from poller ****/
1105
+-            VmSockPosixEventQueueDelete_inlock(
1106
++            VmSockPosixDeleteEventFromQueue(
1107
++            pRESTHandle,
1108
+             pRESTHandle->pSockContext->pEventQueue,
1109
+             pSocket
1110
+             );
1111
+@@ -1001,15 +1050,12 @@ VmSockPosixRead(
1112
+ 
1113
+         if (pSocket->pTimerSocket)
1114
+         {
1115
++            pSocket->pTimerSocket->pIoSocket = NULL;
1116
+             VmSockPosixReArmTimer(
1117
+                 pRESTHandle,
1118
+                 pSocket->pTimerSocket,
1119
+-                ((pRESTHandle->pRESTConfig->connTimeoutSec) * 1000)
1120
++                1
1121
+                 );
1122
+-
1123
+-            write(pSocket->pTimerSocket->fd, "NotifyPQ", 8);
1124
+-            pSocket->pTimerSocket->pIoSocket = NULL;
1125
+-            
1126
+         }
1127
+ 
1128
+     }
1129
+@@ -1191,7 +1237,7 @@ VmSockPosixCloseSocket(
1130
+         if (ret < 0)
1131
+         {
1132
+             errorCode = SSL_get_error(pSocket->ssl, ret);
1133
+-            VMREST_LOG_ERROR(pRESTHandle,"Error on SSL_shutdown on socket %d, return value %d, errorCode %u", pSocket->fd, ret, errorCode);
1134
++            VMREST_LOG_ERROR(pRESTHandle,"Error on SSL_shutdown on socket %d, return value %d, errorCode %u, errno %d", pSocket->fd, ret, errorCode, errno);
1135
+         }
1136
+         SSL_free(pSocket->ssl);
1137
+         pSocket->ssl = NULL;
1138
+@@ -1303,7 +1349,7 @@ VmSockPosixCreateSignalSockets(
1139
+ 
1140
+ static
1141
+ DWORD
1142
+-VmSockPosixEventQueueAdd_inlock(
1143
++VmSockPosixAddEventToQueue(
1144
+     PVM_SOCK_EVENT_QUEUE             pQueue,
1145
+     BOOLEAN                          bOneShot,
1146
+     PVM_SOCKET                       pSocket
1147
+@@ -1337,33 +1383,6 @@ VmSockPosixEventQueueAdd_inlock(
1148
+     return dwError;
1149
+ }
1150
+ 
1151
+-static
1152
+-DWORD
1153
+-VmSockPosixEventQueueDelete_inlock(
1154
+-    PVM_SOCK_EVENT_QUEUE             pQueue,
1155
+-    PVM_SOCKET                       pSocket
1156
+-    )
1157
+-{
1158
+-    DWORD                            dwError = REST_ENGINE_SUCCESS;
1159
+-    struct                           epoll_event event = {0};
1160
+-
1161
+-    if (!pSocket || !pQueue)
1162
+-    {
1163
+-        dwError = REST_ERROR_INVALID_HANDLER;
1164
+-    }
1165
+-    BAIL_ON_VMREST_ERROR(dwError);
1166
+-
1167
+-    if (epoll_ctl(pQueue->epollFd, EPOLL_CTL_DEL, pSocket->fd, &event) < 0)
1168
+-    {
1169
+-        dwError = VM_SOCK_POSIX_ERROR_SYS_CALL_FAILED;
1170
+-        BAIL_ON_VMREST_ERROR(dwError);
1171
+-    }
1172
+-
1173
+-error:
1174
+-
1175
+-    return dwError;
1176
+-}
1177
+-
1178
+ static
1179
+ DWORD
1180
+ VmSockPosixAcceptConnection(
1181
+@@ -1637,7 +1656,8 @@ VmSockPosixSetRequestHandle(
1182
+         }
1183
+ 
1184
+         /**** Delete actual IO socket from poller ****/
1185
+-        dwError = VmSockPosixEventQueueDelete_inlock(
1186
++        dwError = VmSockPosixDeleteEventFromQueue(
1187
++                      pRESTHandle,
1188
+                       pQueue,
1189
+                       pSocket
1190
+                       );
1191
+@@ -1868,7 +1888,7 @@ VmSockPosixCreateTimer(
1192
+                   );
1193
+     BAIL_ON_VMREST_ERROR(dwError);
1194
+     
1195
+-    dwError = VmSockPosixEventQueueAdd_inlock(
1196
++    dwError = VmSockPosixAddEventToQueue(
1197
+                   pRESTHandle->pSockContext->pEventQueue,
1198
+                   TRUE,
1199
+                   pTimerSocket
1200
+@@ -1923,50 +1943,44 @@ VmRESTAcceptSSLContext(
1201
+ 
1202
+     if (ret == 1)
1203
+     {
1204
+-        VMREST_LOG_DEBUG(pRESTHandle," SSL accept successful on socket %d, ret %d, errorCode %u", pSocket->fd, ret, errorCode);
1205
++        VMREST_LOG_DEBUG(pRESTHandle,"SSL accept successful on socket %d, ret %d, errorCode %u", pSocket->fd, ret, errorCode);
1206
+         pSocket->bSSLHandShakeCompleted = TRUE;
1207
+         bReArm = TRUE;
1208
+     }
1209
+     else if ((ret == -1) && ((errorCode == SSL_ERROR_WANT_READ) || (errorCode == SSL_ERROR_WANT_WRITE)))
1210
+     {
1211
+-       VMREST_LOG_DEBUG(pRESTHandle," SSL handshake not completed for socket %d, ret %d, errorCode %u", pSocket->fd, ret, errorCode);
1212
++       VMREST_LOG_DEBUG(pRESTHandle,"SSL handshake not completed for socket %d, ret %d, errorCode %u", pSocket->fd, ret, errorCode);
1213
+        pSocket->bSSLHandShakeCompleted = FALSE;
1214
+        bReArm = TRUE;
1215
+     }
1216
+-    else if ((ret == 0) && (errorCode == SSL_ERROR_SYSCALL))
1217
++    else
1218
+     {
1219
+-         VMREST_LOG_ERROR(pRESTHandle," Client closed the connection during SSL handshake, socket fd %d, ret %d, errorCode %u, errno %d", pSocket->fd, ret, errorCode, errno);
1220
++         VMREST_LOG_ERROR(pRESTHandle,"SSL handshake failed on socket fd %d, ret %d, errorCode %u, errno %d", pSocket->fd, ret, errorCode, errno);
1221
+          dwError = VMREST_TRANSPORT_SSL_ACCEPT_FAILED;
1222
+          BAIL_ON_VMREST_ERROR(dwError);
1223
+     }
1224
+-    else
1225
++
1226
++    if (bReArm && bWatched)
1227
+     {
1228
+-        VMREST_LOG_ERROR(pRESTHandle, "SSL handshake failed...connection will be closed for socket with fd %d, ret %d, errorCode %u, errno %d", pSocket->fd, ret, errorCode, errno);
1229
+-        dwError = VMREST_TRANSPORT_SSL_ACCEPT_FAILED;
1230
++        /**** Rearm and add the socket ****/
1231
++        dwError = VmSockPosixReArmTimer(
1232
++                      pRESTHandle,
1233
++                      pSocket->pTimerSocket,
1234
++                      ((pRESTHandle->pRESTConfig->connTimeoutSec) * 1000)
1235
++                      );
1236
+         BAIL_ON_VMREST_ERROR(dwError);
1237
+-    }
1238
+ 
1239
+-     if (bReArm && bWatched)
1240
+-     {
1241
+-         /**** Rearm and add the socket ****/
1242
+-         dwError = VmSockPosixReArmTimer(
1243
+-                       pRESTHandle,
1244
+-                       pSocket->pTimerSocket,
1245
+-                       ((pRESTHandle->pRESTConfig->connTimeoutSec) * 1000)
1246
+-                       );
1247
+-         BAIL_ON_VMREST_ERROR(dwError);
1248
+-
1249
+-         event.data.ptr = pSocket;
1250
+-         event.events = EPOLLIN;
1251
++        event.data.ptr = pSocket;
1252
++        event.events = EPOLLIN;
1253
+ 
1254
+-         event.events = event.events | EPOLLONESHOT;
1255
++        event.events = event.events | EPOLLONESHOT;
1256
+ 
1257
+-         if (epoll_ctl(pRESTHandle->pSockContext->pEventQueue->epollFd, EPOLL_CTL_MOD, pSocket->fd, &event) < 0)
1258
+-         {
1259
+-             dwError = VM_SOCK_POSIX_ERROR_SYS_CALL_FAILED;
1260
+-             BAIL_ON_VMREST_ERROR(dwError);
1261
+-         }
1262
+-     }
1263
++        if (epoll_ctl(pRESTHandle->pSockContext->pEventQueue->epollFd, EPOLL_CTL_MOD, pSocket->fd, &event) < 0)
1264
++        {
1265
++            dwError = VM_SOCK_POSIX_ERROR_SYS_CALL_FAILED;
1266
++            BAIL_ON_VMREST_ERROR(dwError);
1267
++        }
1268
++    }
1269
+   
1270
+ cleanup:
1271
+ 
1272
+@@ -1977,7 +1991,8 @@ VmRESTAcceptSSLContext(
1273
+     if (pRESTHandle && pRESTHandle->pSockContext)
1274
+     {
1275
+         /**** Delete from poller ****/
1276
+-        VmSockPosixEventQueueDelete_inlock(
1277
++        VmSockPosixDeleteEventFromQueue(
1278
++            pRESTHandle,
1279
+             pRESTHandle->pSockContext->pEventQueue,
1280
+             pSocket
1281
+             );