Socket :shutdown() Support?

I have been reviewing all of my driver code for best practices around socket and have noticed that none of the SmartThings sample drivers, and most of the community drivers, don’t call sock:shutdown(). I understand it isn’t strictly required. Everyone just calls sock:close() and leaves it up to the device (aka server) to handle the ungraceful closure. Is there a reason for this? Any harm in trying to shut it down gracefully?

client:shutdown(mode)

Shuts down part of a full-duplex connection.

Mode tells which way of the connection should be shut down and can take the value:

"both": disallow further sends and receives on the object. This is the default mode;
"send": disallow further sends on the object;
"receive": disallow further receives on the object.
This function returns 1.

I’m more of a Unix/Linux C/C++ guy, so I know it from that side. Always use close()!

Here’s an explanation:

Everything in Unix/Linux is a stream. You want to make sure to close the file handle.

Yes. The question was in addition to the close() call, not instead of.

sock:shutdown() — possibly handle errors
sock:close()

Hi, @blueyetisoftware

I asked the engineering team and they mentioned the call to close() is enough for the Hub to perform the shutdown and the clean up.

1 Like

Thanks. Did they say if the shutdown is being handled internally in the platform or are they saying it doesn’t matter because connected devices should handle it anyway?

They mentioned it is handled in the Hub’s software.

1 Like

Great. Thanks again.

It’s the TCP/IP stack of the Kernel. LuaSocket is just an abstraction layer.

TCP sockets support bidirectional communication, allowing both sending and receiving data through the same socket. The close() function halts communication in both directions entirely. In contrast, shutdown() includes an additional parameter that lets you selectively stop either the sending or receiving direction.

A key distinction (between close() and shutdown() for both directions) is that close() will leave the socket open if it’s being used by another process, whereas shutdown() terminates the socket regardless of other processes sharing it.

This book is still one of the best.

1 Like