stanza icon indicating copy to clipboard operation
stanza copied to clipboard

CoreNLP server doesn't close when using with-statements

Open songyang-dev opened this issue 3 years ago • 1 comments

Describe the bug The CoreNLP server is not stopped automatically after the with statement in Python is finished. This happens during interactive Python sessions and running Python scripts as a whole. The server will close itself if the terminal session is closed.

To Reproduce Steps to reproduce the behavior:

  1. Write
from stanza.server import CoreNLPClient

if __name__ == "__main__":
    text = "Chris Manning is a nice person. Chris wrote a simple sentence. He also gives oranges to people."
    with CoreNLPClient(
        annotators=[
            "tokenize",
            "ssplit",
            "pos",
            "lemma",
            "ner",
            "parse",
            "depparse",
            "coref",
        ],
        timeout=30000,
        memory="6G",
    ) as client:
        ann = client.annotate(text)
  1. Run this code as a script in the terminal
  2. Run this code again as a script in the terminal
  3. See error

Second situation

  1. Write the same code.
  2. Run the code interactively using python -i
  3. Call client.stop()

Expected behavior The server is supposed to be closed at the end of the script or upon calling stop(), according to this demo.

Environment (please complete the following information):

  • OS: Win 11
  • Python version: Python 3.9.5 from Anaconda
  • Stanza version: 1.3.0

Additional context I am running this inside VSCode.

Error message

$ py -3.9 -i extraction/preprocess.py
2022-02-11 17:52:07 INFO: Writing properties to tmp file: corenlp_server-f591e0f5098f4ab5.props
Traceback (most recent call last):
  File "C:\Users\songy\miniconda3\lib\site-packages\stanza\server\client.py", line 132, in start
    sock.bind((self.host, self.port))
OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\songy\Documents\My Documents\UDEM\master thesis\uml data\database\cleaning\3step\extraction\preprocess.py", line 16, in <module>
    with CoreNLPClient(
  File "C:\Users\songy\miniconda3\lib\site-packages\stanza\server\client.py", line 185, in __enter__
    self.start()
  File "C:\Users\songy\miniconda3\lib\site-packages\stanza\server\client.py", line 139, in start
    raise PermanentlyFailedException("Error: unable to start the CoreNLP server on port %d "
stanza.server.client.PermanentlyFailedException: Error: unable to start the CoreNLP server on port 9000 (possibly something is already running there)

songyang-dev avatar Feb 11 '22 22:02 songyang-dev

Is this some issue with Windows not freeing up the port quickly enough? I can run the server multiple times in a row and it works fine for me. No idea about VSCode, though.

One thought that has been in the back of my mind for a while is that there's no reason the functionality of the server can't just be in a subprocess pipe for people who need annotations from a local java process. Aside from the problem that editing the server code was the original inspiration for Dante's Inferno

AngledLuffa avatar Feb 12 '22 07:02 AngledLuffa