8000 Fixed player crash when kick by Mofurka · Pull Request #179 · StarryPy/StarryPy3k · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fixed player crash when kick #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions plugins/player_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ def build_inherits(inherits):

return final

def kick_player(self, player, reason=""):
async def kick_player(self, player, reason=""):
if player.client_id == -1 or player.connection is None:
player.connection = None
player.logged_in = False
Expand All @@ -603,10 +603,16 @@ def kick_player(self, player, reason=""):
self.players_online.remove(player.uuid)
return
try:
world_stop_packet = build_packet(packets["world_stop"],
WorldStop.build(
dict(reason=reason)
))
await player.connection.raw_write(world_stop_packet)
kick_packet = build_packet(packets["server_disconnect"],
ServerDisconnect.build(
dict(reason=reason)))
self.background(player.connection.raw_write(kick_packet))
await player.connection.raw_write(kick_packet)
player.connection.active = False
except AttributeError as e: # Ignore errors in sending the packet.
self.logger.debug("Error occurred while kicking user. {}".format(e))
player.connection = None
Expand All @@ -628,7 +634,7 @@ def ban_by_ip(self, ip, reason, connection):
banned_plr = self.get_player_by_ip(ip, check_logged_in=True)
if banned_plr:
kickstr = "You were kicked.\nReason: {}".format(reason)
self.kick_player(banned_plr, kickstr)
self.background(self.kick_player(banned_plr, kickstr))
ban = IPBan(ip, reason, connection.player.alias)
self.shelf["bans"][ip] = ban
send_message(connection, "Banned IP: {} with reason: {}"
Expand Down Expand Up @@ -955,8 +961,7 @@ async def _kick(self, data, connection):
:return: Null.
"""

# FIXME: Kick is currently broken. Kicking someone will cause their
# starbound client to crash (overkill).
# FIXED: NO MORE CRASHES
try:
alias = data[0]
except IndexError:
Expand All @@ -980,7 +985,7 @@ async def _kick(self, data, connection):
send_message(connection,
"Player {} is not currently logged in.".format(alias))
return
self.kick_player(p, reason)
self.background(self.kick_player(p, reason))
broadcast(self, "^red;{} has been kicked for reason: "
"{}^reset;".format(alias, reason))

Expand Down
14 changes: 11 additions & 3 deletions server.py
CC58
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def __init__(self, reader, writer, config, factory):
self._client_read_future = None
self._server_write_future = None
self._client_write_future = None

self.active = True

logger.info("Received connection from {}".format(self.client_ip))

def start_zstd(self):
Expand Down Expand Up @@ -169,8 +172,13 @@ async def send_message(self, message, *messages, mode=ChatReceiveMode.BROADCAST,
logger.exception(err)

async def raw_write(self, data):
self._writer.write(data)
await self._writer.drain()
if not self.active:
return # stop writing
try:
self._writer.write(data)
await self._writer.drain()
except Exception as e:
logger.error(f"Exception in raw_write: {e}")

async def client_raw_write(self, data):
self._client_writer.write(data)
Expand Down Expand Up @@ -355,4 +363,4 @@ async def main():
try:
asyncio.run(main())
except KeyboardInterrupt:
logger.info("Exited due to interrupt.")
logger.info("Exited due to interrupt.")
8 changes: 6 additions & 2 deletions utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,12 @@ def background(coro):
# To prevent keeping references to finished tasks forever,
# make each task remove its own reference from the set after
# completion:
task.add_done_callback(background_tasks.discard)

def _done_callback(t):
background_tasks.discard(t)


task.add_done_callback(_done_callback)
background_tasks.add(task)
return task


Expand Down
0