Section 16. If it appears from the return of the writ or otherwise that the prisoner is detained on a process under which another person has an interest in continuing his imprisonment or restraint, he shall not be discharged until notice has been given to such other person or his attorney, if within the commonwealth. If such person or his attorney is not within the commonwealth the court may order notice to be given to him.